• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Question about synchronization

 
Greenhorn
Posts: 27
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, all!
I've been studying concurrent programming from Oracle Documentation. And I was testing some code from that tutorial:
I have a class with a field that can be incremented or decremented:

And two Thread classes for create threads: One of them for increment the Counter's value, and the other one for decrement it:


Finally, I coded a main class that creates two threads (IncrementThread and DecrementThread) with the same Counter object, and start them:

I understand when I run this little application that the printed values in console are not consistent:


Decrement thread: value = 8385
Increment thread: value = 8388
Increment thread: value = 8385
Increment thread: value = 8386
Increment thread: value = 8387
Decrement thread: value = 8384
Increment thread: value = 8388



But, what I cannot understand is that something similar happens when I synchronize the access methods to the field "value" of the Counter class (Synchronized Methods):

With synchronized methods I get the same thing:


Decrement thread: value = 1989
Decrement thread: value = 1988
Decrement thread: value = 1987
Decrement thread: value = 1986
Increment thread: value = 1990
Decrement thread: value = 1985
Decrement thread: value = 1985
Decrement thread: value = 1984



Can someone tell me what's happening?
(Details are welcome.)

Thank you, in advance.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You seem to assume that lines 11 and 12 of the DecrementThread and IncrementThread are executed atomically, but they're not. It's perfectly possible (likely, in fact), that threads are switched between lines 11 and 12.
 
Adrian Cordoba
Greenhorn
Posts: 27
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:You seem to assume that lines 11 and 12 of the DecrementThread and IncrementThread are executed atomically, but they're not. It's perfectly possible (likely, in fact), that threads are switched between lines 11 and 12.



Thank you, Ulf Dittmer.
Now, I understand.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand. Can anyone explain details please?
 
Hang a left on main. Then read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic