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…
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…
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…
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…
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…
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…
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…
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
…
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…
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…
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"):…
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…
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…
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…
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…