File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Static Synchronized  method and wait Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Static Synchronized  method and wait" Watch "Static Synchronized  method and wait" New topic
Author

Static Synchronized method and wait

Rajani Deshpande
Ranch Hand

Joined: May 08, 2000
Posts: 45
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

Joined: Jan 30, 2000
Posts: 18670
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 ]

"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Static Synchronized method and wait
 
Similar Threads
Thread synchronization inside static method
synchronized method
Thread locks
Conditions in Synchronized Blocks
Synchronization