• Post Reply Bookmark Topic Watch Topic
  • New Topic

synchronized - wait()

 
kri shan
Ranch Hand
Posts: 1480
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wait() and notify always comes within synchronized block.

synchronized(obj) {

obj.wait();
(or) Thread.wait(obj);
}

Can i give obj.wait() within synchronized method instead of synchronized block ?
 
Vinod Tiwari
Ranch Hand
Posts: 466
1
IntelliJ IDE Java Oracle
 
Henry Wong
author
Sheriff
Posts: 22528
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kri shan wrote:Can i give obj.wait() within synchronized method instead of synchronized block ?


The requirement is that the thread that calls the wait() method must own the lock for the object being used to wait on. It doesn't matter how that lock is acquired.

Henry
 
kri shan
Ranch Hand
Posts: 1480
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hery Wong, How to acquire the lock for the object, if synchronized at the method level(whole method is synchronized) ?
 
Henry Wong
author
Sheriff
Posts: 22528
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kri shan wrote:Hi Hery Wong, How to acquire the lock for the object, if synchronized at the method level(whole method is synchronized) ?


If the thread doesn't own the lock, it will be acquired when the method is called -- and it may block.

If the thread already owns the lock, it will be acquired (again) when the method is called -- and it should not block.

Henry
 
kri shan
Ranch Hand
Posts: 1480
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public synchronized int get() {
while (available == false) {
try {
wait();
} catch (InterruptedException e) { }
}
available = false;
notifyAll();
return contents;
}

I copied this code from web. What is the meaning of using wait() within synchronized get method ?
 
Henry Wong
author
Sheriff
Posts: 22528
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kri shan wrote:public synchronized int get() {
while (available == false) {
try {
wait();
} catch (InterruptedException e) { }
}
available = false;
notifyAll();
return contents;
}

I copied this code from web. What is the meaning of using wait() within synchronized get method ?


Well, without having any context whatsoever, I am going to guess that it is waiting for the contents to be available?

Henry
 
Balaji Ambresh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each object has a lock associated with it.
So, when you just call wait() instread of instance.wait(),
the call is the same as this.wait()
So, instead of creating a seperate lock object, you're making use of the implicit lock within the object.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!