• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Interrupting a Thread

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
consider the following program
class A extends Thread {
public void run() {
try {
synchronized (this) {
wait();
}
} catch (InterruptedException ie) {
System.out.println("isInterrupted() inside catch is " + isInterrupted());
}
}
public static void main(String[] args) {
A a1 = new A();
a1.start();
a1.interrupt();
System.out.println("a1.isInterrupted() inside main is " + a1.isInterrupted());
}
}

--------
one of the output for this program is :
a1.isInterrupted() inside main is true
a1.isInterrupted() inside catch is false

why is this an output. As for the specification of thread method isInterrupted() method never resets the Interrupt flag of a thread.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is given in java doc.

If this thread is blocked in an invocation of the {@link
* Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
* Object#wait(long, int) wait(long, int)} methods of the {@link Object}
* class, or of the {@link #join()}, {@link #join(long)}, {@link
* #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
* methods of this class, then its interrupt status will be cleared and it
* will receive an {@link InterruptedException}.

I think this is the reason why the isInterrupted() is false.
 
Sreeram Desigan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But y is the first isInterrupted() in main immediately after interrupt() returns true.
I guess interrupt() method will set the interrupt status to true and moves thread a1 from waiting status to ready status. The main thread after executing this interrupt on thread a1 will call the isinterrupt() method which will check the status and return true. Now when the thread a1 moves from ready status to execution status it will throw InterruptedException and reset the Interrupt status. Thus the isInterrupted() method in the catch block will return false.
This what i guess has happened but i want a clear proof of it. Thats y i posted this question to know different views on it. But received no replies at all.
 
Vinayagar Karpagam
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried modifying the main() like this :

public static void main(String[] args) {
A a1 = new A();
a1.start();
a1.interrupt();
try{
Thread.sleep(500);
}catch(InterruptedException ex){}
System.out.println("a1.isInterrupted() inside main is " + a1.isInterrupted());
}
}

when i run this program, i got o/p as

first line, catch will print false;
next line, main will print false;

so, without sleep() called, main completes before exception is thrown & prints true.

and with sleep() called, catch completes after exception is thrown & prints false.
 
I brought this back from the farm where they grow the tiny ads:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic