File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes wait() and notify() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "wait() and notify()" Watch "wait() and notify()" New topic
Author

wait() and notify()

Faraah Dabhoiwala
Greenhorn

Joined: May 30, 2007
Posts: 21
which object should be marked synchronized when we want a thread to wait??

how does a thread know which object amongst those which are waiting to notify??


<b>Please don't visit my homepage...</b><br /> <a href="http://shadabworld.110mb.com" target="_blank" rel="nofollow">http://shadabworld.110mb.com</a>
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18138
    
  39

For first question, see my response here...

http://www.coderanch.com/t/233995/Threads-Synchronization/java/synchronization


For second question, most modern JVMs pass the threading to the underlying OS libraries. So it is the OS libraries that choose the waiting thread. As for the Java Specification, it doesn't require any order or preference.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Faraah Dabhoiwala
Greenhorn

Joined: May 30, 2007
Posts: 21
didnt get what you wrote there
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18138
    
  39

Shadab Wadiwala wrote:didnt get what you wrote there


It looks like you reworded you question, after I answered it. I read the previous question as why synchronization was needed.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18138
    
  39

which object should be marked synchronized when we want a thread to wait??


The object that will be used to wait, should be the object that needs to be synchronized.

Henry
Balraj Momi
Ranch Hand

Joined: Jul 23, 2009
Posts: 45

First of all ,you should know that objects don't wait for threads, Its opposite. Threads actually wait for an notification so that they can continue their operation. One thread can own multiple locks but one object lock can only be owned by one thread.

Let me put it this way, following is an example where a machine is waiting for a for a job to work on. Here job is a object and as soon as a new job is build, machine will get notification to continue to work on that job.



Though all of you may agree that, this code is not foolproof, for example notify can happen even before wait starts. This is just to help Shadab understand how threads owns lock on an object and gets notified to reclaim their property .

Regards


Regards
Balraj Kumar
SCJP 5 95%
SCWCD 82%
Preparing for SCBCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wait() and notify()
 
Similar Threads
java biginer
Threads
wait() inside synchronized method
wait/notify deadlock
Interview q