• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

wait() and notifyAll() in static methods

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can I use wait() and notifyAll() in a static method ??
These are non-static methods of the Object class.
Since static methods can't call non-static methods, how is it possible to use these methods in a static method??
Can someone explain in detail..
Thanks
Regards
Balaji
 
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your static method, you have declared the object. Like in the main method:
TestClass t = new TestClass();
t.execute();
The execute() method can be non-static.
Also, wait() and notify() can only be used in synchronized code block.
 
Enthuware Software Support
Posts: 4575
45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting indeed!
You can actually do that, although in a round about way...


When you synch. a static method, the lock is obtained on the 'class' object for that class.
Although contrieved, in above code you can see that you can actually call a wait() on that 'class' object. Without the wait(), it prints "Thread1" and "Thread2" but when you insert wait(), it prints only "Thread1". Because, the first thread calls a wait() in a synch. block. that means, it starts waiting for a lock which is actually owned by itself!!!
The situation is:
The first thread has the lock and then idiotically waits for somebody else to release that lock!
Second thread is stuck because the lock is with the first thread!
So neither of them can run. They're in a deadlock.
It shows two point:
1. You can use wait()/notify()/notifyAll() in static methods.
2. BEWARE OF DEADLOCKS!!!

HTH,
Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus

[This message has been edited by Paul Anil (edited December 08, 2000).]
[This message has been edited by Paul Anil (edited December 09, 2000).]
 
Balaji Sadasivam
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your explanation Adrian. Appreciated.

Regards
Balaji
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul Anil,
Thanks for such a great explanation. I am so confused about synchronized threads , deadlocks situations (like below). Can u PLEASE give an short examples for these & help me understand....???
Awaiting ur response.
Aruna
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think wait and notify can also be used in method which are not synchronized.
I have read it somewhere.
[This message has been edited by Milind Deodhar (edited December 08, 2000).]
[This message has been edited by Milind Deodhar (edited December 08, 2000).]
 
Aru Ven
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Miland,
wait , notify & notifyAll MUST BE IN SYNCHRONIZED BLOCK.
Aruna
 
Milind Deodhar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont know about the block, but I tested and I could use wait(); in a non synchronized method.
Milind
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Milind,
Did your program run ok?
Normally, if you use <code>wait()</code> or <code>notifyAll()</code> in an unsynchronized method the code will compile ok but produces an IllegalMonitorStateException at Runtime.

------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
 
Balaji Sadasivam
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul for your explanation.
Aruna,

Core Java Vol.2 explains the Synchronization/deadlock in detail. It explains both before/after Synchronization with simple example.
Unfortunately, there is no detailed explanation for static synchronized methods..
Balaji
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,
you can not use thread's run to prove your deadlock idea,
if you use start(), no deadlock will happen, use the wait() effect on each thread!!!
 
Paul Anilprem
Enthuware Software Support
Posts: 4575
45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Michael. I meant start() but wrote run()!! Never put untested code
Anyway, I fixed it and I put a sleep() and a println() to make the output more clear. Deadlock will happen if you instert that wait() as nobody is calling a notify()/notifyAll(). Well, it's not actually a deadlock. In crude, deadlock means two threads are waiting for each other to release something that they want.
Classic example is T1 accuires a Printer and then waits for a Scanner without releasing the Printer. T2 accuires a Scanner and then waits for a Printer without releasing the Scanner.
And in this case, neither of them is holding on to anything. Any third party can also call a notify()/notfyAll() (as the lock is already relesed by them). But they are stuck anyway.
-Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the result when I execute the program with c.wait() uncommented. Of course the deadlock occurs after that. But howcome Thread2 can enter into the synchronized code and able to print Thread2 as output?
Thread1
Thread2
 
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic