| 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: 3026
|
|
|
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: 16681
|
|
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 ".
|
 |
 |
|
|
subject: Need Explanation of unpredicted output even if methods are synchronized
|
|
|