Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need Explanation of unpredicted output even if methods are synchronized

 
Nuwan Arambage
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?

 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is different in the output from what you expect?
 
Nuwan Arambage
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20817
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nuwan Arambage
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mr Henry, You learned me what I have missed "grab (and release) the lock per each iteration of the loop ".

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic