• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need Explanation of unpredicted output even if methods are synchronized

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?

 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is different in the output from what you expect?
 
Nuwan Arambage
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ".

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic