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
0
votes
0 answers

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 clear or clone cannot be used for…
Carol
  • 13
  • 5
0
votes
1 answer

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 Compare-and-set feature. Compare-and-set feature…
0
votes
1 answer

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 update(Record other){ this.count = (other.count…
Adwait Kumar
  • 1,552
  • 10
  • 25
0
votes
0 answers

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.
Carol
  • 13
  • 5
0
votes
1 answer

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- counter to be threadsafe, allow counter to be…
grootygroot
  • 57
  • 1
  • 8
0
votes
1 answer

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 AtomicLong API to save my key->value. When all members…
Okay Atalay
  • 71
  • 1
  • 9
0
votes
1 answer

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 implementation. The operations on this variable is…
Jai Pandit
  • 510
  • 1
  • 6
  • 18
0
votes
1 answer

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 wrong with my code? public class Test { public…
zonyang
  • 828
  • 3
  • 10
  • 24
0
votes
2 answers

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 AtomicInteger(); private AtomicInteger b = new…
newlearner
  • 149
  • 1
  • 11
0
votes
1 answer

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 to use it in code but I have no idea what to do in…
Zane Pace
  • 3
  • 4
0
votes
2 answers

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 Provider{ private…
ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
0
votes
1 answer

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 thread wait for next statement when it can't edit the…
0
votes
1 answer

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 be executed concurrently. I used a…
wwwwan
  • 407
  • 1
  • 4
  • 12
0
votes
0 answers

C++: Proper way to share value (data) between different threads

I have the following Thread.cpp: while (1) { rplidar_response_measurement_node_t nodes[8192]; size_t count = _countof(nodes); op_result = drv->grabScanData(nodes, count); if (IS_OK(op_result)) { …
Jerwin Prabu
  • 336
  • 1
  • 3
  • 16
0
votes
2 answers

ExecutorService and AtomicInteger : RejectedExecutionException

I want atomicInteger to have a value of 100 then the program terminates public static void main(String[] args) throws InterruptedException { ExecutorService executor = Executors.newSingleThreadExecutor(); AtomicInteger atomicInteger…
Touya Akira
  • 67
  • 1
  • 7
1 2 3
8 9