aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Multithreading: Are two threads performing the same job? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Multithreading: Are two threads performing the same job?" Watch "Multithreading: Are two threads performing the same job?" New topic
Author

Multithreading: Are two threads performing the same job?

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
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...


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18977
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rohit Ramachandran
Ranch Hand

Joined: Oct 05, 2010
Posts: 102
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

Joined: Sep 20, 2010
Posts: 3649
    
  17

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

Joined: Oct 05, 2010
Posts: 102
Ah okay okay. I get it.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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

Joined: Oct 05, 2010
Posts: 102
341342?
Rohit Ramachandran
Ranch Hand

Joined: Oct 05, 2010
Posts: 102
Yep, just checked.
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
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

Joined: Sep 20, 2010
Posts: 3649
    
  17

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.
 
 
subject: Multithreading: Are two threads performing the same job?