Repository navigation
thread-safe-counter-in-java-a-comprehensive-guide #382
Replies: 3 comments 2 replies
|
Originally posted by Jack on August 27, 2023 in Foojay.io Discussions. No, just no! You did not explain anything! |
|
Originally posted by D on August 28, 2023 in Foojay.io Discussions. The second example with lock looks like not correct. It reads count without using lock. If a reading thread is running on a different CPU from updating thread it is possible that getCount returns stale value from local CPU cache. This example may work in most cases but fail on some CPU, OS. The first example correctly use synchronized to ensure that the latest counter value is returned. |
|
Originally posted by Piotr on August 9, 2025 in Foojay.io Discussions. Why did you choose to implement the counter providing two methods—one for incrementing and the second for reading? In most cases I would like to use the value of the counter just after the increment operation. I can i.e., assign it as a unique ID. If I use the get operation it will return only the recent value without any assurance of its uniqueness. |
Uh oh!
There was an error while loading. Please reload this page.
thread-safe-counter-in-java-a-comprehensive-guide
https://foojay.io/today/thread-safe-counter-in-java-a-comprehensive-guide/
Comments below were imported from the previous foojay.io WordPress site.
All reactions