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

Specify binary digits length for Java AtomicInteger

Java AtomicInteger is using the regular 4 bytes length of int. If we run the following code AtomicInteger i = new AtomicInteger(Integer.MIN_VALUE); System.out.println(i); i.decrementAndGet(); System.out.println(i); We will…
Yifei
  • 1,944
  • 1
  • 14
  • 20
0
votes
0 answers

Those "set" methods in AtomicInteger

I am aware that compareAndSet() in AtomicInteger is a core method that takes a clever approach to atomically update the current value of the integer primitive that AtomicInteger encapsulates. It does so only after it has ascertained that another…
softwarelover
  • 1,009
  • 1
  • 10
  • 22
0
votes
1 answer

Concurrent Thread-safe AtomicInteger

I've read through the API documentation of the java.util.concurrent package, but have obviously misunderstood something. The overview says A small toolkit of classes that support lock-free thread-safe programming on single variables. However, a…
andrew-g-za
  • 967
  • 8
  • 13
0
votes
4 answers

AtomicXXX lazySet for single writer thread

I have an application with a single writer thread which does some stuff and updates some metrics e.g. counters etc. The application has a number of other threads which read the stats and do stuff with them. It's not essential that the metrics are up…
PaddyD
  • 1,097
  • 1
  • 9
  • 19
0
votes
2 answers

GetAndDecrement Much Slower than GetAndIncrement.

I have found that in Java using AtomicIntegers, GetAndDecrement is much slower than GetAndIncrement. Why does this happen?
Strin
  • 677
  • 9
  • 15
0
votes
1 answer

C++ how to compare atomic_ints?

My target system has g++ 4.6.3 which supports C++0x (but not C++11). I am using an atomic_int to store a state variable that I access between two threads. However, there doesn't seem to be a not equals operator defined for this type. How do I…
fred basset
  • 9,774
  • 28
  • 88
  • 138
0
votes
6 answers

Does AtomicInteger really produce atomic integer?

I googled for AtomicInteger and I saw someone said we can use AtomicInteger(AtomicLong) for memory sequencer (http://www.cs.hut.fi/u/tlilja/multicore/slides/java_multicore.pdf). Here is my test: public class TestAtomicInteger { public static void…
LHA
  • 9,398
  • 8
  • 46
  • 85
-1
votes
1 answer

Grouping based on preferences keep returning null value

public class HomeroomMix { public void calculate(String className, int number) { List people = Arrays.asList( new PeopleClass("DE", "male"), new PeopleClass("DE", "female"), …
Steven Oh
  • 382
  • 3
  • 14
-1
votes
1 answer

When I use the volatile and AtomicInteger practice found that the output should have the output of the code disappears

When I use the volatile and AtomicInteger practice found that the output should have the output of the code disappeared, I hope someone can help me solve this problem It should have output, but the output is gone,the code is here: package…
-1
votes
2 answers

Atomic function inside While

could someone explain me about this code please? public class Counter { private AtomicInteger value = new AtomicInteger(); public int incrementLongVersion(){//THIS PART2!!! int oldValue = value.get(); …
Asker
  • 109
  • 7
-2
votes
1 answer

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); FindIterable iterable =…
-2
votes
1 answer

ConcurrentHashMap and AtomicInteger example in Java

I am trying to implement the following feature: each key in str array should be associated with an Integer which starts from 0 and will be stored in map. after execution the map should contains all keys in str and the count should be consistent with…
-2
votes
2 answers

How to use AtomicInteger?

I have a class, for example: public class A{ private final int number; public A(int number){ this.number = number; } } Quesiton is, I want to update number time by time, and I must make A object stateless, which means nubmer…
-2
votes
2 answers

Core Java , Shared Counter in Multithreading env

I got the java counter problem: two threads share a common counter and increment it in turns. Now can someone please show code examples of it using synchronization, Locks and AtomicIntegers means using different approach. I am not getting any good…
pooja
  • 23
  • 2
  • 6
-3
votes
1 answer

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 compareAndSwapInt method is thread-safe,so Why not just change…
1 2 3
8
9