• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multithreading: Are two threads performing the same job?

 
Sandra Bachan
Ranch Hand
Posts: 434
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the code from Sun Web Learning Center




According to Sun, it will print 3 4 1 and will never terminate because "notify is issued before wait"

I can understand that the output is


3 4


But how on Earth does it print 1?


Is this because two threads are performing the same job, i.e. Waiting (which IS-A Runnable). So the two threads share the same copy of flag? Much like two people are editing the same Word document online.

Please clarify.


Also, I don't understand how notify() is issued before wait(). Where can notify be placed so that the waiting thread will receive notification that lock is now free? I'm guessing notify should be out of the synchronized block because you can only have one thread at a time...
 
Henry Wong
author
Marshal
Pie
Posts: 20829
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
Sandra Bachan wrote:
But how on Earth does it print 1?


Remember that the two threads are sharing variables, so one thread is affecting the other. Figure that out first.


Sandra Bachan wrote:
Also, I don't understand how notify() is issued before wait().


I wouldn't bother with this just yet... because if you can't figure out how "1" is printed, then you can't figure out how the wait() method is even called.

Henry
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry is it because an IllegalMonitorStateException is thrown since the thread doesn't hold a lock on the object? After the 1 is printed?
 
Stephan van Hulst
Bartender
Pie
Posts: 4826
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it's because the second thread calls wait(), and there's nothing to wake it up. It's essentially a deadlock.
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah okay okay. I get it.
 
Stephan van Hulst
Bartender
Pie
Posts: 4826
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It gets pretty interesting if you run a third thread. Without running the code yourself, can you tell me what the output is when the main method looks like this?
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
341342?
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, just checked.
 
Sandra Bachan
Ranch Hand
Posts: 434
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Remember that the two threads are sharing variables, so one thread is affecting the other. Figure that out first.



Two threads are sharing the variable flag, hence only one of them can get into the synchronized block. Am I correct?
 
Stephan van Hulst
Bartender
Pie
Posts: 4826
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Only one of them can get into the synchronized block at a time, because the entire object (referenced by w) is locked, not because they look at the same flag.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic