Questions tagged [atomic-long]

Anything related to the Java `AtomicLong` data type, an integer type that can be accessed and updated atomically.

Anything related to the Java AtomicLong data type, an integer type that can be accessed and updated atomically.

See Java API AtomicLong documentation.

21 questions
53
votes
1 answer

What is AtomicLong in Java used for?

Can some one explain what AtomicLong is used for? For example, what's the difference in the below statements? private Long transactionId; private AtomicLong transactionId;
Nilotpal
  • 3,237
  • 4
  • 34
  • 56
14
votes
4 answers

CAS vs synchronized performance

I've had this question for quite a while now, trying to read lots of resources and understanding what is going on - but I've still failed to get a good understanding of why things are the way they are. Simply put I'm trying to test how a CAS would…
Eugene
  • 117,005
  • 15
  • 201
  • 306
6
votes
1 answer

How does incrementAndGet method of AtomicLong works internally?

I am using incrementAndGet method of AtomicLong in my multithreaded code to measure the performance of some of our client side code. @Override public void run() { long start = System.nanoTime(); attributes =…
arsenal
  • 23,366
  • 85
  • 225
  • 331
6
votes
4 answers

AtomicLong operations

I need to perform the following operation: // average, total, elapsed are Long's average = ( ( total * average ) + elapsed ) / (++total); But I want to use AtomicLong This is what I'm trying but I don't quite get if it is correct: average.set(…
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
4
votes
1 answer

How to use AtomicLong on Hazelcast CP Subsystem with just 2 Hazelcast cluster nodes?

I am updating to latest Hazelcast version [3.12] and I am facing problem to obtain a instance of AtomicLong. The new version, HZ introduces the concept of CAP Theorem, to grant Consistency and Partition Tolerance, but he problem is the CP subsystem…
3
votes
1 answer

LongAdder Striped64 wasUncontended implementation detail

This is a question not about how LongAdder works, it's about an intriguing implementation detail that I can't figure out. Here is the code from Striped64 (I've cut out some parts and left the relevant parts for the question): final void…
Eugene
  • 117,005
  • 15
  • 201
  • 306
3
votes
4 answers

is there any advantage of using AtomicLong if you are only setting and getting the value?

I have a need for a long instance variable. This variable will hold some event time (time in milliseconds). If i'm only setting values into this long and getting the value, is there any advantage of using AtomicLong (and only its get() and set())…
inor
  • 2,781
  • 2
  • 32
  • 42
3
votes
1 answer

The operator += is undefined for the argument type(s) int, AtomicLong

I have a Map as below - ConcurrentHashMap histogram = new ConcurrentHashMap(); This map contains lot of key value pair. Putting the AtomicLong as the value was necessary so that is the reason I have put like that…
user1813228
2
votes
2 answers

is an AtomicLong field in a service a good way to implement server-side an identifier to persist entities through a repository

I am working on a SpringBoot application. The strict requirement I have is to generate server-side a numeric id for an entity and then persist it through the repository. Since each @Service is stateless and so a singleton, is the usage of an…
2
votes
1 answer

Redisson getAtomicLong - what happens if key does not exist?

I'm building a cache implementation in Java using Redisson. I want to use it to cache a numerical value. So I'm using getAtomicLong() like so: RAtomicLong userNumber = redissonClient.getAtomicLong("my-key"); long value = userNumber.get(); However,…
Tyler M.
  • 115
  • 1
  • 1
  • 11
1
vote
1 answer

Atomic assignment of long value in java

I have the use case to update epoch time of events in a long variable. This variable will have lots of concurrent reads and writes as well. Here are the requirements in detail: Very fast completion of reads and writes Reads may or may not return…
Manas Saxena
  • 2,171
  • 6
  • 39
  • 58
1
vote
1 answer

why my own AtomicLong are slower than the one provide in the JDK?

I was writing my own AtomicLong class and I just found that the function I had is much slower than the one provided in the Unsafe class. I am wondering why? Below are the codes I have: public interface Counter { void increment(); long…
1
vote
2 answers

How to ensure the correctness of AtomicLong addAndGet result

I want calculate current percentage in my muli-thread download programme.But there is a strange problem . The lastDownloadSize during the second download must be the sum of write and lastDownloadSize of lastDown. example There is my code private…
luckylo
  • 45
  • 4
1
vote
0 answers

Which class should take care of doing a get on an AtomicLong

I have a multithreaded application where I need to ensure all threads are referring to the latest version of a long. I was thinking of using AtomicLong for this purpose. The AtomicLong will be in one class and other classes will need to get the…
Kirit
  • 389
  • 2
  • 3
  • 11
1
vote
1 answer

Does getAndAdd method in AtomicLong cause threads waiting?

I have a program running multi-thread, all thread share and process on single AtomicLong variable. Each of them will call getAndAdd() method first to retrieve value and process further. If all threads running simultaneously, does calling above…
Thong Vo
  • 809
  • 1
  • 9
  • 21
1
2