• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

notifyAll()

 
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ();
}
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
abalfazl, I share the same understanding as you. This is what I think the original codes is doing.
 
Marshal
Posts: 80780
489
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Too difficult a question for "beginning". Moving thread.
 
Campbell Ritchie
Marshal
Posts: 80780
489
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80780
489
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic