• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

wait method

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
i have a doubt in this,
class A extends Thread {
public void run() {
try {
synchronized (this) {
wait();
}
} catch (InterruptedException ie) {
System.out.print(interrupted());
}
}
public static void main(String[] args) {
A a1 = new A();
a1.start();
a1.interrupt();
}
}
is it possible that before a1 has got the opportunity to run, a1.interrupt is called? in that case what wud happen for the wait called in run() method and the out put for as a result?
thnkx,
basanti
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is
----------------
class A extends Thread {
public void run() {
try {
synchronized (this) {
wait();
}
} catch (InterruptedException ie) {
System.out.print(interrupted());
}
}
public static void main(String[] args) {
A a1 = new A();
a1.start();
a1.interrupt();
}
}
is it possible that before a1 has got the opportunity to run, a1.interrupt is called?
------
Yes it is possible .
------------
in that case what wud happen for the wait called in run() method and the out put for as a result?
----------
In that case wait() method will retun immediately and exception will be handled.Since you used System.out.xxx in the exception handling section(exception handling will clear the interrupt status first),
System.out.print(interrupted());
it will result "false".
If the code looks like following, the result would have been "true " followed by "false".
class A extends Thread {
public void run() {
try {
synchronized (this) {
System.out.print(isInterrupted());
wait();
}
} catch (InterruptedException ie) {
System.out.print(isInterrupted());
}
}
public static void main(String[] args) {
A a1 = new A();
a1.start();
a1.interrupt();
}
}

Note : interrupted() clears the interrupt status after its call. So calling it twice for a interrupted thread will result "true false" isInterrupted() does not clear the interrupt status.
You can excute this few times, you will see the result yourself.
Ambapali
 
We find this kind of rampant individuality very disturbing. But not this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic