• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Multithreading: Are two threads performing the same job?

 
Ranch Hand
Posts: 434
  • 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...
 
author
Posts: 23833
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • 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
 
Ranch Hand
Posts: 102
  • 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?
 
Saloon Keeper
Posts: 10300
217
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah okay okay. I get it.
 
Stephan van Hulst
Saloon Keeper
Posts: 10300
217
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
341342?
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, just checked.
 
Sandra Bachan
Ranch Hand
Posts: 434
  • 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
Saloon Keeper
Posts: 10300
217
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!