• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static Synchronized method and wait

 
Rajani Deshpande
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
If I have a static synchronized method, can I never release the lock till i finish the method?
Because , wait and notify cannot be used in a static method...
Is this right???
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have a static synchronized method, can I never release the lock till i finish the method?
Yes, unless you call wait() (see below), which releases the lock. Of course, you could use a synchronized block within the method which has the same effect as declaring the method synchronized - except that you can control when it starts and ends:

I used the MyClass.class object as the monitor, because that's what the monitor would have been if you'd used a static synchronized method (assuming it's in the class MyClass).
Because , wait and notify cannot be used in a static method...
They can, if you use an object to call them. I.e. you can't use just plain wait() since that means this.wait() and there's no "this" in a static method. But you can call someOtherObject.wait(). Inside a static synchronized method, you could release the lock with

[ December 14, 2002: Message edited by: Jim Yingst ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!