Questions tagged [atomicinteger]
Java's AtomicInteger class
115
questions
0
votes
3
answers
56
views
AtomicInteger & lambda expressions in single-threaded app
I need to modify a local variable inside a lambda expression in a JButton's ActionListener and since I'm not able to modify it directly, I came across the AtomicInteger type.
I implemented it and it ...
0
votes
1
answer
50
views
Compare and swap value in atomic integer kotlin
Hey I am learning atomic integer in kotlin. I want to know is there atomic integer swap value if current value is smaller than new value. For example
AtomicInt 10
Scenario 1 new value is 5 and it ...
9
votes
2
answers
326
views
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 ...
0
votes
2
answers
57
views
Atomic Variables Accessed within inner classes
So I am doing a sort here and implementing the Comparator interface using an inner class, exceptionMessage and didJsonParsingFailed are variables declared outside the inner class, now java doesn't ...
0
votes
2
answers
307
views
Java 8 variable should be final or effectively final issue [duplicate]
I am using Java 8 stream Iteration with a variable that should be used in other classes also. So I have used the below code.
AtomicBoolean bool = new AtomicBoolean(true);
public void testBool(){
list....
0
votes
1
answer
15
views
How to do comparison and adding AtomicInteger
There is such synchronized function:
private int balance;
//...
public synchronized void executeRequest(Request request) {
switch (request.getType()) {
case CREDIT:
...
1
vote
1
answer
190
views
Java Volatile variable still causing race condition [duplicate]
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 ...
1
vote
1
answer
41
views
Thread safety if loop is thread safe [duplicate]
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 ...
3
votes
1
answer
162
views
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 ...
0
votes
0
answers
45
views
Is there a way to access the date added to an ArrayList in a different to change its data?
I have this in the Class: public static ArrayList<String> studentInfo = new ArrayList<String>();I created a method CreateStudent() that basically allows the user to create a student using ...
0
votes
1
answer
170
views
Java auto increment from base
In my case I have class and subclass. Problem is when I search my function increment id automatically. When I first time click search it upload 5 ids, when I add one and go back to search it upload 5 ...
-4
votes
1
answer
51
views
What is the meaning of the spin in the getAndIncrement method in the AtomicInteger class?
In the getAndIncrement method of the AtomicInteger class,the getAndAddInt method of the unsafe class is called,which contains the compareAndSwapInt method and the spin(do...while).
The ...
3
votes
2
answers
65
views
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 ...
1
vote
1
answer
134
views
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 ...
0
votes
1
answer
68
views
How to observe AtomicInteger variable value change in android?
I am using AtomicInteger in my android app. Now I want to listen to AtomicInteger variable value changes, how can I listen?
private final AtomicInteger hostsCount = new AtomicInteger(0);
...
1
vote
2
answers
89
views
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 ...
0
votes
1
answer
246
views
When AtomicInteger is faster than synchronized
I've already read a great number of articles where is said that AtomicInteger class works faster than a synchronize construction. I did some tests on AtomicInteger and "synchronized" and in ...
0
votes
2
answers
44
views
Atomic Integers values are not getting updated in threads
I have defined an atomicinterger variable in class. The class also extends thread class .I have created two threads and incrementing the value of the atomic integer in run method .I was excepting the ...
0
votes
0
answers
53
views
BitSet in parallel stream in java
I wanted to ask that how we can efficiently use a thread-safe bitset in parallel java. I mean is there any alternatives for bitset. First, I tried to use AtomicIntegerArray. However, methods like ...
0
votes
1
answer
89
views
How does a thread using AtomicInteger does less number of context switching?
I was studying about AutomaticInteger. It stated that the use of an AtomicInteger makes an integer operation non-blocking. It is said that the compareAndSet() method of the AtomicInteger makes use of ...
0
votes
1
answer
42
views
Atomically update 2 Long values
Let's say I have the following class in Java:
class Record {
String name;
double count;
long repeat;
public Record(String name){
this.name = name;
}
public synchronized void ...
3
votes
1
answer
61
views
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 ...
2
votes
2
answers
66
views
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?
2
votes
1
answer
147
views
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 ...
0
votes
0
answers
28
views
AtomicIntegerArray versus BitSet in terms of performance
I wanted to ask about the efficiency of using AtomicIntegerArray and BitSet, and which one has better performance when used in parallel stream java?
Thank you in advance for your help.
-1
votes
1
answer
51
views
Grouping based on preferences keep returning null value
public class HomeroomMix {
public void calculate(String className, int number) {
List<PeopleClass> people = Arrays.asList(
new PeopleClass("DE", "male"),
...
2
votes
2
answers
51
views
Map<Object, AtomicInteger> 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<Object, AtomicInteger>, I'm converting it to the following object:
{1=2, ...
0
votes
1
answer
810
views
Thread safe counter- Atomic Integer not working
I have (say) 10 threads. All threads increment the counter and the same will be logged.
The counter is an atomic integer but sometimes I am getting duplicate values in the counter.
What I expect is- ...
2
votes
1
answer
1k
views
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, == ...
1
vote
1
answer
250
views
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....
-2
votes
1
answer
84
views
Does this construction make sense?
Faced a project with this code:
public class IndexUpdater implements Runnable {
@Override
public void run() {
final AtomicInteger count = new AtomicInteger(0);
...
0
votes
1
answer
181
views
Hazelcast AtomicLong Data loss When multiple member left
Hazelcast fails when multiple members disconnect from the cluster.
My scenario is so basic and my configuration has 3 bakcup option(it does not work). I have 4 members in a cluster and i use ...
0
votes
1
answer
27
views
UI Thread Bound AtomicInteger
I have been reading about SMPs machines(x86/ARM)and Compiler reordering for efficiency. Now I want to make a counter and the variable lives as a private member of ActivityLifecycleCallbacks ...
1
vote
1
answer
241
views
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 >= ...
0
votes
1
answer
154
views
Not getting expected result when updating AtomicInteger variable in multiple threads
In this code I'm using 10 threads updating an AtomicInteger variable. I expect the final result of Counter.getInstance().holder.n to be 1000000, but it prints out random number like 991591.
What's ...
0
votes
2
answers
91
views
Is a Thread-safe method with two AtomicIntegers possible?
I want to write a Thread-safe method sum() but I'm not sure if I can use two AtomicIntegers to make it safe or do I have to use a synchronized block?
class A {
private AtomicInteger a = new ...
0
votes
1
answer
305
views
How to get AtomicInteger to work in a Runnable class
I wrote this code as a Runnable in Java, but my professor wants me to add AtomicInteger so that there is no thread interference. How do I go about doing that? I have tried looking up examples of how ...
1
vote
0
answers
429
views
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 ...
0
votes
3
answers
1k
views
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, ...
1
vote
2
answers
67
views
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....
0
votes
1
answer
1k
views
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) "...
0
votes
2
answers
146
views
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; <---- ...
0
votes
2
answers
198
views
Using AtomicInteger and CountDownLatch to generate one instance
I have a class which I want to have only one instance. However, I don't want multiple threads to call getInstance(). So I have coded it in the following way
public class SomeService implements ...
2
votes
4
answers
413
views
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 ...
3
votes
3
answers
460
views
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 ...
0
votes
1
answer
41
views
Does the thread wait until it can edit (by example addAndGet) an atomic variable?
I have two threads and one atomic integer. Both run a loop of 1000 iterations. One Thread increments by one the other decrements by one. The result is at the end 0.
Good so far.
But does a) one ...
1
vote
1
answer
41
views
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 ...
3
votes
1
answer
726
views
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 ...
5
votes
1
answer
225
views
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 ...
0
votes
1
answer
90
views
Does it really make sense to synchronize map.get(key) while the value is a AtomicLong when we want to use map.get(key).wait()?
What I am trying to do is to implement a key-specific read-write lock. Multiple read requests can be executed concurrently if there is no write request on that key. Put requests on different keys can ...