• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing Threads and Diffrent Types of method Passing in "synchronize"  RSS feed

 
Greenhorn
Posts: 15
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How to wake up a specific thread, assume multiple threads are sleeping ? Also what is the difference between "synchronize(A.class)", "synchronize(A.this)" and "synchronize(this)"
 
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can not wake up any specific thread in the pool of several sleeping threads..

synchronize(A.class) means you are getting lock on class level, synchronize(this) means you are getting lock on local object, synchronize(A.this) means getting lock on outer class.
Let our experts comments on it..
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Kesharwani wrote:How to wake up a specific thread, assume multiple threads are sleeping ? Also what is the difference between "synchronize(A.class)", "synchronize(A.this)" and "synchronize(this)"

I believe the normal method is with a notify(); however, waking specific threads comes under the heading of "micro-management" and is not generally recommended.

As to the rest: I suggest you read the tutorials, because multi-threading is not simple, and knowing why is much more important than knowing how.

Winston
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree you should find the tutorials. The notify method is only reliable if all waiting objects are of the same type and waiting to do the same thing. Otherwise you would try notifyAll.
 
Sheriff
Posts: 4298
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I was taught is that you should always use notifyAll(). With notify(), there is a chance that the thread you try to wake up will be busy.
 
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
Knute Snortum wrote:What I was taught is that you should always use notifyAll(). With notify(), there is a chance that the thread you try to wake up will be busy.


Ahhhh.... "busy" doing what?? The notify() method wakes up a thread that is waiting -- so, the thread is only busy waiting for the notification.

Henry
 
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
Campbell Ritchie wrote:Agree you should find the tutorials. The notify method is only reliable if all waiting objects are of the same type and waiting to do the same thing. Otherwise you would try notifyAll.


Only partially agree. Using the notifyAll() method is only a quick fix. The correct fix is to use a different notification object -- or if the different notification objects require the same synchronization lock, try using the ReentrantLock class with multiple Condition objects for notifications.

Henry
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!