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
1
vote
2 answers

Why is count1 < count2 after running the following code? Both count1 and count2 should be 2000000

Compile and run in JDK11 and look at the results. sum1 is the time taken by the synchronized code and sum2 is the time for the AtomicInteger code. count1 is the result of counting the number of calls to the synchronized count++. count2 is the same…
1
vote
1 answer

Need to understand the problem in AtomicInteger code usage in multithreaded environment

In one of the interview, a coding question was asked to me and I had to find the problem in that code and suggest proper solution. Please find below the entire code: import java.util.concurrent.ExecutorService; import…
Ruchi
  • 41
  • 6
1
vote
1 answer

java AtomicInteger accumulateAndGet is applied

I have a Counter class that stores value as AtomicInteger. That class should be thread safe. I have method boolean consume(int number) that should decrement counter and return true if counter >= number, and should not change counter and return false…
a3dsfcv
  • 1,146
  • 2
  • 20
  • 35
1
vote
0 answers

Minimum number of swaps needed to getting the queue to final state not exceeding 2 swaps per element

Scenario or Problem statement: It's New Year's Day and everyone's in line for the Wonderland rollercoaster ride! There are a number of people queued up, and each person wears a sticker indicating their initial position in the queue. Initial…
1
vote
3 answers

How to write a Java sequence generator for UniqueId, which will Auto-Reset to '1' (base seq. number) at midnight everyday

We have a Spring boot service where we receive files everyday, because of some issue (on producer) we are receiving multiple files with same name & date appended. The new file overwriting the old, to handle it we want to append a sequence (starting…
Shri
  • 57
  • 7
1
vote
2 answers

Is ordered execution expected on the single thread

Since the service is single threaded, the monkey1 loop series will always get executed before monkey2, so we can expect monkey1 will always be greater than monkey2, can't we? import java.util.concurrent.*; public class MonkeyCounter { private…
Treefish Zhang
  • 1,131
  • 1
  • 15
  • 27
1
vote
1 answer

Which AtomicInteger methods are test-and-set, fetch-and-add and compare-and-swap (in terms of lock-free algorithms)?

CAS (compare-and-swap) : boolean compareAndSet(int expect, int update) FAA(fetch-and-add) : int addAndGet(int delta) ??? TAS (test-and-set) : ??? In my understanding: CAS (compare-and-swap) "synchronizes" (w/o locks, on CPU instructions level) code…
Code Complete
  • 3,146
  • 1
  • 15
  • 38
1
vote
2 answers

Is it necessary to use AtomicInteger in a ThreadFactory?

I think it's necessary to use AtomicInteger in the ThreadFactory but when I am trying to prove it to myself, I failed hard. new ThreadFactory() { private int threadId = 0; <---- AtomicInteger preferred @Override …
Hearen
  • 7,420
  • 4
  • 53
  • 63
1
vote
1 answer

Why is this code not thread-safe even though an AtomicInteger is used to track progress?

I tried making a class that extends thread which simply takes an array of strings and prints the first 2 strings out alternately for 10000 iterations. I keep track of the index to print from using an AtomicInteger (counter), however the output…
Amelia
  • 417
  • 1
  • 4
  • 12
1
vote
4 answers

Threads with shared integer object not working as expected

I have a problem where i have to print the numbers in such format. First 1 First 2 Second 3 Second 4 First 5 First 6 Second 7 Second 8 First 9 and so on... I have implemented my runnable interface as below. class ThreadDemo implements…
agnihp
  • 13
  • 3
1
vote
1 answer

Java calculation in Atomic

I have a small question about Java AtomicInteger. I know that I can realise thread-safe counters. But I couldn't find anything about complex calculations with AtomicInteger. For example I have this calculation (i and j are object-vars, "this"):…
user3849220
1
vote
0 answers

Appropriate atomic pointer to use in this case

Let's say I have an implementation of the Herlihy-Wing Queue in Java : public class HWQueue { AtomicReference[] items; AtomicInteger tail; static final int CAPACITY = 1024; public HWQueue() { items…
Jarvis
  • 8,494
  • 3
  • 27
  • 58
1
vote
1 answer

Java atomic variables native / internal implementation

How does Java's atomic variables like AtomicInteger work internally to achieve mutual exclusion/atomicity? Is there any locking involved at machine instruction level which yields better performance? Or an atomic machine level instruction itself does…
Xavier DSouza
  • 2,861
  • 7
  • 29
  • 40
1
vote
1 answer

How to get a unique value in Java that corresponds to uint32_t?

I have to generate a unique number in Java (for my machine on which the code is running) so that in C++ it can correspond to uint32_t. In general other C++ program should be able to read this unique number properly as uint32_t. I am sending this…
john
  • 11,311
  • 40
  • 131
  • 251
1
vote
1 answer

Using AtomicInteger with JMH multi-thread?

I am using JMH to test some features of my project. When I try to use it's @GroupThreads with AtomicInteger, I cannot reset the AtomicInteger, it just increases over time. I also tried with if else to check and reset AtomicInteger but cannot. Could…
hminle
  • 501
  • 1
  • 8
  • 19
1 2 3
8 9