Questions tagged [atomicinteger]

Java's AtomicInteger class

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/atomic/AtomicInteger.html

java.util.concurrent.atomic.AtomicInteger was new in Java 1.5

121 questions
4
votes
2 answers

AtomicInteger thread safety

How can I achieve that the while loop here is always executed exactly 100 times. When I execute the code, in rare cases it prints 99 or 98 lines on the console and not always 100, which I do not understand. import java.util.ArrayList; import…
Anne Maier
  • 299
  • 1
  • 8
4
votes
1 answer

Could you make a ReadWriteLock using just an AtomicInteger for locks?

If you used bit masks to store read and write lock in a single AtomicInteger, could you realize a fast ReadWriteLock class? How would it be different from a regular ReentrantReadWriteLock?
Riki137
  • 2,076
  • 2
  • 23
  • 26
4
votes
0 answers

Why does Java 8 provide a LongAccumulator but not an IntAccumulator?

Currently, my program makes heavy use of AtomicIntegers in order to keep track of running maximums of ints that are reported in parallel. After reading about Java 8's new LongAccumulators, I had hoped to use an IntAccumulator with max as the…
4
votes
1 answer

AtomicInteger is slower than synchronized

I testes how fast the Atomic Integer in multithread with comparing synchronized method, but I got the result that Atomic Integer was slower than synchronized method. I read Java API Reference and I understood Atomic Integer is faster than…
user2738844
  • 169
  • 3
  • 10
4
votes
2 answers

How should I use AtomicInteger as a reference counter to manage some resource?

I've refined this question a little, removing some of my understanding to try and make it as concise and specific as poss so it might come across as pretty basic How should I use AtomicInteger as a reference counter to manage clean-up of some…
wmorrison365
  • 5,995
  • 2
  • 27
  • 40
3
votes
2 answers

Java AtomicInteger not equal to a million after a million iterations (minimal example included)

I am writing a program which computes the squares of integers between 0 and 1 million(not inclusive) in an array. I am using up to 8 threads(inclusive). For indexing the array i am using an atomic integer. I expect the for loop body in the run…
3
votes
1 answer

Does AtomicInteger handle synchronization?

If two threads both increment same int i using i++, we could get a problem, as i++ is not atomic operation. That is why there is AtomicInteger.increment(), which makes incrementing atomic. So if we had 1 core and 2 threads doing .increment(), there…
Ana Maria
  • 475
  • 2
  • 11
3
votes
4 answers

atomic integer counter printing in wrong order

I have defined the following instance variable: private final AtomicInteger tradeCounter = new AtomicInteger(0); I have a method called onTrade defined as below being called by 6 threads: public void onTrade(Trade trade) { …
user3809938
  • 1,114
  • 3
  • 16
  • 35
3
votes
3 answers

How does AtomicInteger is thread-safe

I was reading some documents about Atomic variables in Java. As written everywhere, AtomicInteger is supposed to be thread-safe. As per my understanding of atomic integers, it works on the principle of Compare and Swap algorithm. I am unable to…
Geek_To_Learn
  • 1,816
  • 5
  • 28
  • 48
3
votes
1 answer

Difference between Atomic set() and getAndSet() methods in Java

In one of my programs, I was trying to update the value of an Atomic Integer, but could not decide between set() and getAndSet() methods because they both seem to do the same. I have already gone through this and this post, but they are comparing…
Steve
  • 889
  • 1
  • 12
  • 26
3
votes
1 answer

Ordering a list of 10 numbers

I have an atomic integer array of size 10. I am using this array to organize numbers 1-10 sent in by threads. This 1-10 will eventually be able to change to be a range of numbers larger than 10 and the list is to contain the 10 greatest numbers in…
Brooke
  • 109
  • 1
  • 2
  • 12
3
votes
1 answer

Multiple threads and AtomicInteger

I took this code from a tutorial on atomics, it stated :- "By using AtomicInteger as a replacement for Integer we're able to increment the number concurrently in a thread-safe manor without synchronizing the access to the variable. The method…
Chris Milburn
  • 862
  • 1
  • 12
  • 20
3
votes
1 answer

Is JVM allowed to reorder instructions around AtomicInteger calls

Is it safe to assume that the code before (AtomicInteger a).addAndGet(-1) will be always executed before this call, i.e. JVM will not reorder the instructions around the addAndGet call? If yes, is it safe to assume that other threads checking the…
Gedrox
  • 3,592
  • 1
  • 21
  • 29
3
votes
4 answers

AtomicInteger incrementation not behaving as expected

I was reading about AtomicInteger and how its operations are atomic and how these properties make it useful for multithreading. I wrote the following program to test the same. I am expecting the final size of the set should be 1000, since each…
Kapil Raju
  • 685
  • 1
  • 8
  • 12
2
votes
2 answers

Volatile keyword on Atomic Variable

The following code is valid in Java volatile AtomicInteger a = new AtomicInteger(123); Do we require volatile keyword on Atomic variables such as AtomicInteger? Or is volatile superfluous?
Kumar
  • 1,536
  • 2
  • 23
  • 33
1
2
3
8 9