This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

notifyAll()

 
abalfazl hossein
Ranch Hand
Posts: 635
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 635
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 635
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl, I share the same understanding as you. This is what I think the original codes is doing.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47262
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult a question for "beginning". Moving thread.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47262
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47262
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 443
3
C++ Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic