• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static Synchronized method and wait

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
A "dutch baby" is not a baby. But this tiny ad is baby sized:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic