aspose file tools*
The moose likes Java in General and the fly likes final void notifyall():Wakes up all threads that are waiting for this objects lock. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "final void notifyall():Wakes up all threads that are waiting for this objects lock." Watch "final void notifyall():Wakes up all threads that are waiting for this objects lock." New topic
Author

final void notifyall():Wakes up all threads that are waiting for this objects lock.

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1056

Hi,


final void notifyall():Wakes up all threads that are waiting for this objects lock.

this sentence is confusin me

does that means that it wakes up the object that was locked?

Can any one please elaborate in a simpler manner..


http://www.lifesbizzare.blogspot.com || OCJP:81%
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1056

Hi,

Please reply
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

Hi,

PatienceIsAVirtue. I would think you would know that by know, after 287 posts.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
There can be any number of waiting threads ready to continue but that are waiting for the thisObject lock.
From another thread (and synchronized on thisObject) calling thisObject.notify() will make one of the waiting
threads runnable. Calling thisObject.notifyAll() will make all the waiting threads runnable and the JVM will start
allocating time to each. This code launches 10 threads. It then calls notify() followed by notifyAll().
Jim ... ...



BEE MBA PMP SCJP-6
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Vishal Hegde wrote:Hi,
does that means that it wakes up the object that was locked?


Object won't go to sleep....
If a Thread got the lock for a particular object, then that Thread doing some works on that object, and other Threads, which want that object lock to do their job, is waiting, and the notifyAll() method notifies that the currently working Thread has finished its job. So others can try to acquire the lock of that particular object.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Remember that there is only one logical CPU to share across all the threads. Notice
that main() launches the new threads in code that is not synchronized. So it completes
the 10 launches as it is granted time to do so. As each new thread enters run() and is granted
the shared lock, wait() is called. This suspends the thread and releases the lock. The next new
thread does the same. (Each thread is paused in the middle of synchronized code.)

Meanwhile, the main thread now wants the lock also, to call notify(). (The sleep(200) is included
to help assure that all 10 threads are waiting before the main thread gets the lock. There is no
guarantee of this but it seems to work.) main() calls notify(), releases the lock, and immediately
asks for it again.

It is interesting that the lock is now granted to one of the 10 waiting threads rather than giving
it back to main(). The selected thread picks up where it left off; it prints the "continue..." message,
releases the lock and dies. At this point the other 9 threads are still paused, waiting to regain
the lock, but the single notify() has been consumed. The lock can now be granted back to main()
where notifyAll() is called and the lock is released again.

Abimaran : I think this is where your question comes in. The JVM sees that notifyAll() has been
called, and the lock is available, so it grants it to another of the paused threads. When this thread
completes its work and releases the lock, it is granted to another, and so-on until all the paused
threads have been restarted.

Calling notify() causes the JVM to pick just one paused thread, give it the lock and stop. For
notifyAll(), the JVM continues down its list of waiting threads as the lock becomes available.

Jim ... ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final void notifyall():Wakes up all threads that are waiting for this objects lock.