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
33
votes
2 answers

How does "Compare And Set" in AtomicInteger works

AtomicInteger works with two concepts : CAS and volatile variable. Using volatile variable insures that the current value will be visible to all threads and it will not be cached. But I am confused over CAS(compare AND set) concept which is…
Onki
  • 1,879
  • 6
  • 38
  • 58
18
votes
5 answers

Performance Difference of AtomicInteger vs Integer

Is there any performance difference between AtomicInteger and Integer?
Boni
  • 590
  • 1
  • 3
  • 11
14
votes
2 answers

Is there any functional difference between AtomicInteger.updateAndGet() and AtomicInteger.accumulateAndGet()?

Is there any scenario in which AtomicInteger.accumulateAndGet() can't be replaced with AtomicInteger.updateAndGet(), or is it just a convenience for method references? Here's a simple example where I don't see any functional…
shmosel
  • 49,289
  • 6
  • 73
  • 138
11
votes
3 answers

How to update an Atomic based on a condition?

How to update an AtomicInteger if its current value is less than the given value? The idea is: AtomicInteger ai = new AtomicInteger(0); ... ai.update(threadInt); // this call happens concurrently ... // inside AtomicInteger atomic…
Stephan Rozinsky
  • 553
  • 2
  • 6
  • 21
10
votes
2 answers

Is each cycle of a for loop an atomic operation?

Is the incrementAndGet method of the following AtomicBigInteger implementation an atomic operation? I'm particularly wondering about the for (; ; ) part. Does the JVM somehow guarantee that each cycle in a for loop is executed atomicly? public final…
eztam
  • 3,443
  • 7
  • 36
  • 54
10
votes
4 answers

Why is AtomicInteger needed if writes and reads to int variables are atomic?

I've read in Oracle docs that: Reads and writes are atomic for reference variables and for most primitive variables (all types except long and double). (I guess this feature has been added in some new JDK release because I used to think that…
frostman
  • 566
  • 2
  • 12
  • 25
8
votes
3 answers

Why AtomicInteger based Stream solutions are not recommended?

Say I have this list of fruits:- List f = Arrays.asList("Banana", "Apple", "Grape", "Orange", "Kiwi"); I need to prepend a serial number to each fruit and print it. The order of fruit or serial number does not matter. So this is a valid…
Kartik
  • 7,677
  • 4
  • 28
  • 50
7
votes
4 answers

AtomicInteger in multithreading

I want to find out all the prime numbers from 0 to 1000000. For that I wrote this stupid method: public static boolean isPrime(int n) { for(int i = 2; i < n; i++) { if (n % i == 0) return false; } return true; } It's…
dimedrol90
  • 276
  • 1
  • 4
  • 12
6
votes
1 answer

compareandexchange() vs compareandset() of Atomic-Integer

While working on AtomicInteger, I found this API provides two Methods. compareAndExchange: Atomically sets the value to newValue if the current value, referred to as the witness value, == expectedValue, with memory effects as specified by…
T-Bag
  • 10,916
  • 3
  • 54
  • 118
6
votes
1 answer

How to implement/use atomic counter in Metal fragment shader?

I want to implement an A-Buffer algorithm for order-independent-transparency in my Metal application. The description of the technique mentions using an atomic counter. I've never used one of these or even heard of them. I just read about atomic…
bsabiston
  • 721
  • 6
  • 22
5
votes
1 answer

Initializing an atomic_flag

I have a struct, let's call it struct foo, to which I'd like to add an atomic_flag variable. So far, I've been callocing the struct given that it mostly needs to be zero initialized. How should I be initializing the atomic_flag member? struct foo{ …
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
5
votes
1 answer

AtomicInteger and Compare operations

I'm trying to use an AtomicInteger variable as lock. So, wondering if the code posted below is thread safe. I know incrementAndGet() is an atomic operation. But, I'm not sure if the subsequent '==' operation will be atomic as well (what if the value…
V1666
  • 185
  • 3
  • 14
5
votes
4 answers

Is Atomic Integer incrementAndGet() thread safe?

Is Atomic Integer incrementAndGet() method thread safe? I don't see any use of synchronized keyword in it. I am using following code to generate the unique id: public enum UniqueIdGenerator { INSTANCE; private AtomicLong instance = new…
Aman
  • 1,170
  • 3
  • 15
  • 29
5
votes
2 answers

AtomicInteger.compareAndSet that returns the original value, not a boolean

Java's AtomicInteger offers public final boolean compareAndSet(int expect, int update). If false is returned, I would like to know what the value actually was at the time when the comparison failed. Is this possible in Java? In .Net, there's public…
Evgeniy Berezovsky
  • 18,571
  • 13
  • 82
  • 156
5
votes
1 answer

Is it good practice to use AtomicInteger as a substitute for mutable Integer?

I have following kind of scenario: It is simple form of code that I am showing here to clarify my concern: public void callMe() { AtomicInteger howManyOdds = new AtomicInteger(0); AtomicInteger howManyEvens = new AtomicInteger(0); …
Mac
  • 1,711
  • 3
  • 12
  • 26
1
2 3
8 9