• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Thread synchronization

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Output for above code:
Thread[Thread-0,5,main] Before
Thread[Thread-1,5,main] Before
Thread[Thread-1,5,main] a = 2
Thread[Thread-0,5,main] a = 3
Thread[Thread-1,5,main] After
Thread[Thread-0,5,main] After


Isn't this output strange, since the synchronized keyword (with the method) must disallow simultaneous execution of the method by both the threads?
I expected an output as follows:

Thread[Thread-0,5,main] Before
Thread[Thread-1,5,main] a = 2
Thread[Thread-1,5,main] After
Thread[Thread-1,5,main] Before
Thread[Thread-0,5,main] a = 3
Thread[Thread-0,5,main] After
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
t1 and t2 are two instances of Thread1, so they are free to run in any order (I mean the way they gets their turn). t1 is free to execute it's print method while t2 execute it's own.
 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're running totally different instances of the same class. That is t1 and t2 shares only int a = 1, but nothing more!
That's why printing ouput is like that.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello N Pats,

Make the print() method static, then you will get your desired output.
For making static instance variable synchronized,make the method synchronized.


Rishan Shahrier
SCJP 5.0
 
Natalie Ap
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jari Timonen wrote:You're running totally different instances of the same class. That is t1 and t2 shares only int a = 1, but nothing more!
That's why printing ouput is like that.





Hello,

Please have a look at the above code. It works fine to give me the expected output. But i'd really appreciate if someone could explain why this is working.. This time i have 2 different instances or 2 different classes and both are sharing the same object v of class Shared.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason why it works now is because there is only one lock involved (the lock for the shared Shared instance.)
Edit: I just took another look at your code. What output are you expecting, and what output are you getting? To make things more interesting, print the id of the thread after each print() invocation in the for() loop and see what you get.
 
Natalie Ap
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:The reason why it works now is because there is only one lock involved (the lock for the shared Shared instance.)
Edit: I just took another look at your code. What output are you expecting, and what output are you getting? To make things more interesting, print the id of the thread after each print() invocation in the for() loop and see what you get.




Hi Ruben,

Earlier, i was getting the following output despite of sing the word synchronized. I was using it at the wrong place and was getting the following output:


Output for above code:

Thread[Thread-0,5,main] Before
Thread[Thread-1,5,main] Before
Thread[Thread-1,5,main] a = 2
Thread[Thread-0,5,main] a = 3
Thread[Thread-1,5,main] After
Thread[Thread-0,5,main] After


instead of this output:(expected)

Thread[Thread-0,5,main] Before
Thread[Thread-1,5,main] a = 2
Thread[Thread-1,5,main] After
Thread[Thread-1,5,main] Before
Thread[Thread-0,5,main] a = 3
Thread[Thread-0,5,main] After

Now i understood concept.

Thanks all!! But more inputs/explanations are welcome..
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic