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
2
votes
1 answer

Concurrency issue when coordinating two threads

I am trying to divide the work of a loop into two threads. I am using an ExecutorService to create the 2nd thread while using the main thread as the other one. I am using a counter to stop the loop when it reaches a certain value but I'm not able to…
2
votes
2 answers

Map to Associative array

I have for input int[] with the following content: [5, 65, 22, 1, 58, 5, 69, 12, 1, 22, 22, 58, 12, 54, 89] Using Map, I'm converting it to the following object: {1=2, 65=1, 5=2, 69=1, 22=3, 58=2, 12=1} In other words, I'm…
2
votes
3 answers

Java8: Issue on running a Thread example

I came across the following example on winterbe.com which is demonstrating the use of Atomic variables. // From http://winterbe.com/posts/2015/05/22/java8-concurrency-tutorial-atomic-concurrent-map-examples/ public class Test_AtomicInteger { …
Patrick C.
  • 1,339
  • 3
  • 16
  • 31
2
votes
2 answers

AtomicInteger conditional check thread safe

I ran across a doubt if below is thread safe, // is this thread safe, final int MAX_COUNT = 3 ? if (retryCount.get() < MAX_COUNT) { // some other code retryCount.getAndIncrement(); } else { // reset count & some other code …
Vijay
  • 384
  • 4
  • 16
2
votes
2 answers

not able to control atomic behavior of AtomicInteger while subtracting value

This is the first time that I was using java.util.concurrent.atomic package for synchronization purpose in multithreading. I was trying to handle the classic example of AccountDanger class given in Kathy Sierra book for OCP through…
JPG
  • 1,247
  • 5
  • 31
  • 64
2
votes
1 answer

About java.util.concurrent AtomicInteger

I came across source code of AtomicInteger class on GrepCode and found following code snippet. static { try { valueOffset = unsafe.objectFieldOffset (AtomicInteger.class.getDeclaredField("value")); } catch (Exception ex) {…
2
votes
1 answer

Practical example of some AtomicInteger's methods

Could you please help me to grasp the gist of some methods of AtomicInteger class: updateAndGet, accumulateAndGet. Why the first one recieves IntUnaryOperator as a parameter? What logic can potentially be applied in functional method of this…
void
  • 731
  • 2
  • 11
  • 26
2
votes
1 answer

Copying ConcurrentHashMap to HashMap inconsistency

im trying to do the following: My program starts threads, which increment an AtomicInteger from ConcurrentHashMap and adds new Integer to ConcurrentHashMap>. In that case, the size of the…
2
votes
3 answers

Can anyone explain why we use "final AtomicInteger count = this.count;", and why use keyword final

public E poll() { final AtomicInteger count = this.count; if (count.get() == 0) return null; E x = null; int c = -1; final ReentrantLock takeLock = this.takeLock; takeLock.lock(); try { if (count.get() >…
Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
1
vote
2 answers

Can we achieve visibility and atomicity using Volatile and Synchronized together?

I am just trying to understand... Will I achieve the same result using the below two approaches? I mostly see the first approach only. Is there anything wrong with the second approach? We can use AtomicInteger to achieve the same but just wanted to…
itspr
  • 273
  • 1
  • 3
  • 6
1
vote
1 answer

How to use AtomicInteger getAndSet correctly?

I want to get the difference between consecutive list elements using Java stream and AtomicInteger: List list = List.of(1,2,3,5,6,8,9,10,22,23,27); AtomicInteger diff = new AtomicInteger(1); List result = list.stream() …
wannaBeDev
  • 516
  • 3
  • 14
1
vote
1 answer

ForkJoinFramework with AtomicLong is not giving consistent result

I'm trying to play with ForkJoinFramework. I know that my code might not be a good use case to use ForkJoin, but it should work at least.. I'm using RecursiveAction to concurrently modify a static AtomicLong variable. But I'm having some issues…
Kyle Yuan
  • 25
  • 4
1
vote
1 answer

Java Volatile variable still causing race condition

I was running an experiment on different approaches to deal with race condition in multi-threaded java applications . Strategies like atomic variables, synchronize worked well , but I dont see the issue being solved when using volatile variables.…
1
vote
1 answer

Thread safety if loop is thread safe

The while loop in this question is thread safe: AtomicInteger thread safety. If I insert the randomSum method in the thread safe while loop, is the code still thread safe? Or should the randomSum method be synchronized? import…
Anne Maier
  • 299
  • 1
  • 8
1
vote
1 answer

Strange output using Atomic Integer in Producer Consumer Problem in Java

While implementing Producer-Consumer Problem in Java using BlockingQueue, getting strange output using AtomicInteger, where 2 threads are producing the same result and putting the same counter variable in the queue. How are that output possible…
Rohit Narang
  • 55
  • 1
  • 12
1 2
3
8 9