• Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads, wait method an spurious wakeup  RSS feed

 
Yeray Santana Borges
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From K&B, chapter 9, question 9, page 749:

9. Given the following
<blockquote>code:
<pre name="code" class="core">
public class WaitTest {
public static void main(String [] args) {
System.out.print("1 ");
synchronized(args){
System.out.print("2 ");
try {
args.wait();
}
catch(InterruptedException e){}
}
System.out.print("3 ");
} }
</pre>
</blockquote>
What is the result of trying to compile and run this program?
A. It fails to compile because the IllegalMonitorStateException of wait() is not dealt
with in line 7.
B. 1 2 3
C. 1 3
D. 1 2
E. At runtime, it throws an IllegalMonitorStateException when trying to wait.
F. It will fail to compile because it has to be synchronized on the this object.

Answer:
D is correct. 1 and 2 will be printed, but there will be no return from the wait call because
no other thread will notify the main thread, so 3 will never be printed. It's frozen at line 7.


But, what happen with the spurious wakeup??, is possible that the the main thread don�t wait forever for a notify(), a spurious wakeup success and the output will be B. 1 2 3 too?

I don�t understand the spurious wakeup, affect to any threads?, maybe te main thread not is affected?

Sorry for my poor english, thanks in advance

[ July 16, 2008: Message edited by: Yeray Santana Borges ]
[ July 16, 2008: Message edited by: Yeray Santana Borges ]
 
Keith Nagle
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if I could speak Spanish as well as you
can type English I would be a happy camper!
Anyway, yes, main will freeze when it calls the wait
method on the String array object.
It will remain in the waiting state until another
thread calls either notify() or notifyAll().
OR! If you look at java.lang.Object in the api you will find
another wait method that looks like:
wait(long timeout) where timeout specifies the amount of milliseconds
that the thread will wait before proceeding if notify() or notifyAll()
are not invoked on the object that wait was initially invoked on.
Hope that helps!
Best regards to ya
 
kamal shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While working on your problem,I end up with the below code.
Just want to share with you might it will help you to clarify wait,notify and lock of threads.

Please run the below and see how it works.
<blockquote>code:
<pre name="code" class="core">

public class WaitTest implements Runnable {

public static Object a =new Object();

public static void main(String [] args) {

System.out.print("Our Program Starts Now");
/*****Same lock will needed to notify************/
a=(Object)args;
/*****Same lock will needed to notify************/

/********To wakeup the Main Thread************/
WaitTest tObj=new WaitTest();
Thread t=new Thread(tObj);
t.start();
/********To wakeup the Main Thread************/

synchronized(args){
System.out.print("Going to get wait on args lock "+args);
try {
args.wait();
} catch(InterruptedException e){System.out.println("Exception in getting wait on agrs lock"+e);}

}

System.out.print("Wait has been over!! Thanks");


}

public void run(){
release();
}

public void release(){

//Insert your code here once done,it will notify the main thread.

synchronized(a){
System.out.println("Going to help main thread"+a);
try{

System.out.println("Calling notify to release main thread");
a.notify();
}catch(IllegalMonitorStateException e){System.out.println("Exception in calling notify "+e);}

}
System.out.println("Call of notify Done");
}
}



</pre>
</blockquote>
 
Raphael Rabadan
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by kamal shah:
While working on your problem,I end up with the below code.
Just want to share with you might it will help you to clarify wait,notify and lock of threads.

Please run the below and see how it works.
...


Hello, kamal shah

Your example can get stuck. Just to show you, add it to get the chance higher:

<blockquote>code:
<pre name="code" class="core">
WaitTest tObj = new WaitTest();
Thread t = new Thread(tObj);
t.start();
/** ******To wakeup the Main Thread*********** */
/** COPY FROM HERE - Making the chance of get stuk higher! */
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
/** TO HERE - End **/
synchronized (WaitTest.class) {
System.out.println("Going to get wait on args lock " + args);
</pre>
</blockquote>

Raphael Rabadan
 
kamal shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes,you are right if the notify get called before the wait,it would get stuck.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!