This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes notifyAll() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "notifyAll()" Watch "notifyAll()" New topic
Author

notifyAll()

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


http://www.javaworld.com/javaworld/jw-07-2002/jw-0703-java101.html?page=7


1-The only thread that uses ock.notifyAll (); is main thread who sleeps for 3000.Why synchronized is used here?

synchronized (lock) {
lock.notifyAll ();
}
Sze Kong Chan
Greenhorn

Joined: Mar 30, 2011
Posts: 10

The only thread that uses ock.notifyAll (); is main thread who sleeps for 3000.Why synchronized is used here?
synchronized (lock) {
lock.notifyAll ();
}


Although, only the main thread call lock.notifyAll(), but all 4 threads (including main) uses the lock object. Whenever any thread access a shared object, it is only safe to synchronized them. Because we don't know what the method do to the attributes within the shared object

Hope this help.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
These three threads, A, B , C , runs the code in run method in class MyThread.

I changed the code, But it stops:





A before wait
B before wait
C before wait
main thread sleeping
main thread awake
Sze Kong Chan
Greenhorn

Joined: Mar 30, 2011
Posts: 10
That is because you have change the MyThread.run to wait on each thread objects themselves. After which, nothing is going the notify them to wake up :0)..

In the original implementation, all of the MyThread instances are waiting on the same lock object, and the main thread will wake all of them up to print the "after wait" statement.

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Let me tell you what I understand from the first code:

1- 3 Threads are created.

2- These three threads uses one lock," o".

3- These threads lose lock, Because they call wait method.

4-Main thread take the "o" lock.

5-Main thread calls notifyAll()

6-These three methods wake up!


Please correct me if I am wrong.





Sze Kong Chan
Greenhorn

Joined: Mar 30, 2011
Posts: 10
abalfazl, I share the same understanding as you. This is what I think the original codes is doing.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37923
    
  22
Too difficult a question for "beginning". Moving thread.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37923
    
  22
Did you actually get that code from the Javaworld website?

I think you are correct in your understanding. I think the reason the main thread calls the notifyAll method is because the main thread is deciding when they should be woken, ie after the 3000ms wait.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37923
    
  22
By the way: I think you should tell us what it is you don't understand in the Javaworld article; we can help you better if we know that.
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 407
    
    1

PS Don't use wait outside of a loop (its bad form) see http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#wait%28%29


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: notifyAll()
 
Similar Threads
why when try won't throw a exception ,it can not compile?
Another Question (.... Threads / join() )
small mistake in ExamLab
Please help.
help regarding synchronized methods