aspose file tools*
The moose likes Threads and Synchronization and the fly likes Need Explanation of unpredicted output even if methods are synchronized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Need Explanation of unpredicted output even if methods are synchronized " Watch "Need Explanation of unpredicted output even if methods are synchronized " New topic
Author

Need Explanation of unpredicted output even if methods are synchronized

Nuwan Arambage
Ranch Hand

Joined: May 05, 2010
Posts: 76
Hi everyone,
I have written a simple code fragment to learn threading concepts.I have attached code I have written.


This is the shared object that contains the count variable among threads.This is my thread class


This is my Main class


So after executing this program.I mentioned the output I have got.Out put varies when I ran the program several times.

C:\Documents and Settings\narambage\Desktop\HelloWOrld\Threads\Volatile>java Main
Main Thread is Completed main
Increment 1 thread1
Increment 2 thread1
Increment 3 thread1
Increment 4 thread1
Increment 5 thread1
Increment 6 thread1
Increment 7 thread1
Increment 8 thread1
Increment 9 thread2
Increment 10 thread2
Increment 11 thread2
Increment 12 thread2
Increment 13 thread2
Increment 14 thread2
Increment 15 thread2
Increment 16 thread1
Increment 17 thread1
Increment 18 thread1
Increment 19 thread1
Increment 20 thread1
Increment 21 thread1
Increment 22 thread1
Increment 23 thread1
Increment 24 thread1
Increment 25 thread2
Increment 26 thread2
Increment 27 thread2
Increment 28 thread2
Increment 29 thread2
Increment 30 thread2
Increment 31 thread2
Increment 32 thread2
Increment 33 thread2
Increment 34 thread1
Increment 35 thread1
Increment 36 thread1
Increment 37 thread2
Increment 38 thread2
Increment 39 thread2
Increment 40 thread2

Q: Why do I get a unpredictable out put even if I synchronized a incrementCount() method ?


Thinker
Nuwan Arambage
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

What is different in the output from what you expect?


Steve
Nuwan Arambage
Ranch Hand

Joined: May 05, 2010
Posts: 76
hi steve,

I was expecting a this kind of output.

C:\Users\Nuwan Arambage\Desktop\HelloWorld\Threads\sync>java Main
Main Thread is Completed main
Increment 1 thread1
Increment 2 thread1
Increment 3 thread1
Increment 4 thread1
Increment 5 thread1
Increment 6 thread1
Increment 7 thread1
Increment 8 thread1
Increment 9 thread1
Increment 10 thread1
Increment 11 thread1
Increment 12 thread1
Increment 13 thread1
Increment 14 thread1
Increment 15 thread1
Increment 16 thread1
Increment 17 thread1
Increment 18 thread1
Increment 19 thread1
Increment 20 thread1
Increment 21 thread2
Increment 22 thread2
Increment 23 thread2
Increment 24 thread2
Increment 25 thread2
Increment 26 thread2
Increment 27 thread2
Increment 28 thread2
Increment 29 thread2
Increment 30 thread2
Increment 31 thread2
Increment 32 thread2
Increment 33 thread2
Increment 34 thread2
Increment 35 thread2
Increment 36 thread2
Increment 37 thread2
Increment 38 thread2
Increment 39 thread2
Increment 40 thread2

C:\Users\Nuwan Arambage\Desktop\HelloWorld\Threads\sync>

however I won't get it by executing previously mentioned code.when I change code as follows(changes to synchronized block & run method) I get the output what I need.





Question is this, Why wouldn't I get the expected output by executing previously mentioned code.

Nuwan Arambage
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40

Nuwan Arambage wrote:Question is this, Why wouldn't I get the expected output by executing previously mentioned code.


Let's reverse the question. Why do you expect to get the expected output by executing the first set of code? Why do you expect the whole loop to be atomic, when you grab (and release) the lock per each iteration of the loop?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Nuwan Arambage
Ranch Hand

Joined: May 05, 2010
Posts: 76
Thanks Mr Henry, You learned me what I have missed "grab (and release) the lock per each iteration of the loop ".

 
Consider Paul's rocket mass heater.
 
subject: Need Explanation of unpredicted output even if methods are synchronized