• Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads implementation  RSS feed

 
Raj chiru
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi......
suppose if we have the twenty threds (T1,T2.....T20) are in wait state, i want to notify particular thread T6,so How we can implement in java?
 
Pawan Arora
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can either use notify or notifyAll method to notify your thread. Both methods is in java.lang.Object class.
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawan Arora wrote:You can either use notify or notifyAll method to notify your thread. Both methods is in java.lang.Object class.

I disagree. If you use notify(), only one thread will be notified, no specific thread. If you use notifyAll(), suprise suprise, all waiting threads at this object/lock will be notified. But you don't know which thread will be the chosen one to start running.

Bob
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Threads that are waiting on the same object should be somewhat interchangeable -- meaning any thread could wake up, when using notify(), and it would be fine. If this is not possible... then using notifyAll() is generally used to wake them all up, which means the threads have to be coded to work it out among themselves as to who is supposed to take the notification.

If you are using Java 5 or later, you can also use the concurrent library instead. The Lock class, can be used to replace the synchronization mechanism, and the Condition class can be used to do the notification. The advantage here is that you can have more than one Condition per Lock, allowing you to target notification for specific conditions.

Henry
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you'll have to use a trick or something to do that. You can't notify a particular thread so you might interrupt that particular thread. There is another trick in my mind but it is very dirty so I won't mention it here ...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!