• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Lost Update Problem with Concurrent Thread

 
Ranch Hand
Posts: 94
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.
I'm reading Head First Java, the part about threads and synchronization.
I'm afraid I didn't get the simple example on "Lost Update" problem.

Here is the code:


which gives a result like:
- Not Synch -
balance 1
balance 3
balance 4
balance 2
balance 6
balance 5
balance 7
balance 8
balance 9
...
balance is not in synch as expected. So far it's ok.

Then the book say to synchronize the 'increment' method to make it atomic.



Which gives me:

- In Synch -
balance 1
balance 2
balance 4
balance 5
balance 3
balance 6
balance 7
balance 8
balance 9
balance 10
balance 12
balance 11
balance 14
balance 15
balance 13
...

But the balance remains unsynchronized.

So what's the point to mark 'increment' method synchronized?
 
Marshal
Posts: 65466
249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you know the synchronisation isn't working? How do you know the problem isn't with System.out; your updates might be correctly synchronised and the printing can be out of time with it. If there is failure to synchronise, you will either get the same value printed twice or one value omitted from the printouts.
 
Daniele Barell
Ranch Hand
Posts: 94
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so, Campbell, is there a method to verify the synchronization?
 
Campbell Ritchie
Marshal
Posts: 65466
249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try adding the balance variables to a List<Integer> every time that method is called. Then see whether each List reads [0, 1, 2...50]
Note the strange code in lines 22‑23 which breaks the increment into two parts, so it is not atomic. Please check in the Java® Language Specification whether something like i++; is atomic or not.
 
Daniele Barell
Ranch Hand
Posts: 94
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You can try adding the balance variables to a List<Integer> every time that method is called. Then see whether each List reads [0, 1, 2...50]



Good idea!
Thank you!
 
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order to see synchronization works properly just move inside method: just right after
Your code increments in synchronized mode, but prints result asynchronously right now.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, to someone who is here,

So, why does not the balance at the end of the “for ”cycle come to a lesser value at some times?

for 50 cycle, it come to 100.

Because of the “lost update “,why the value of balance still is right at the end?

Many thanks , I come from China, can’t find answer within Chinese web.
 
Saloon Keeper
Posts: 10539
224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the increment is synchronized. There's no way for one thread to miss it when another thread has incremented the variable. It's just printed out of order because the printing is not synchronized.

In the version of the code where nothing is synchronized, there may be lost updates because one thread overwrites the increment of another.
 
Nir Wang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Because the increment is synchronized. There's no way for one thread to miss it when another thread has incremented the variable. It's just printed out of order because the printing is not synchronized.

In the version of the code where nothing is synchronized, there may be lost updates because one thread overwrites the increment of another.



In the version of the code with synchronized,I do understand the final value of balance will be 100(for 50 cycles).

But in the version of the code where nothing is synchronized,the final value of balance should not be 100,right? (but I run the code on my computer,the final sentence is”balance 100” )
 
Stephan van Hulst
Saloon Keeper
Posts: 10539
224
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, not "should not". "Might not". There's no guarantee it will have any particular value. It's still likely that the final value will be 100, depending on your system and how the OS or the JVM schedules threads.

Try it with 10.000 increments, and without the print statement, and then see what the final value is.
 
Nir Wang
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:No, not "should not". "Might not". There's no guarantee it will have any particular value. It's still likely that the final value will be 100, depending on your system and how the OS or the JVM schedules threads.

Try it with 10.000 increments, and without the print statement, and then see what the final value is.



Thank you very much!🙏
I get it! I have tried it ,it worked!

we’d apply Heisenberg’s Uncertainty Principle to pretty much everything related to threads.😁
 
Stephan van Hulst
Saloon Keeper
Posts: 10539
224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad it worked
 
The only cure for that is hours of television radiation. And this tiny ad:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!