• 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

why not result effect with same method

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class ThreadMotion extends Thread
{

public void run(){
for ( int i=0; i<6 ; i++){
System.out.println("am I interrupted: " + currentThread().isInterrupted() );
//why not "true" after the if() block here???
if ( i==2 ){
System.out.println("tm now sleep: ");
try{
this.sleep(100000);
}catch(InterruptedException e){
System.out.println("tm interrupted by main thread, so interrupted now: "
+ this.isInterrupted() );
//why not "true" after here???
} //
}//if
}//for

}//run
public static void main(String[] args)
{

ThreadMotion tm= new ThreadMotion( );

System.out.println("name is: " + tm + " start run: ");
tm.start();

try{ sleep(3000); }catch(InterruptedException e){}
System.out.println("now main wake up and try to interrupt tm: ");
tm.interrupt();
System.out.println("check from main if tm is interrupt: " + tm.isInterrupted() ); //interrupted here "true"
}//main

}//ThreadMotion
//I am very cofused why the isInterrupted not return the same "true" result after the excetption???
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nice piece of code you wrote there, an Interesting issue by all means.
Though in order to solve this riddle you should understand two things;
1) Simply enough, interrupted State is not persistant.
2) It vanishes once either you have called interrupted() or you have caught InterruptedException, its the later because of which when you call isInterrupted() its issues false; rightly as Thread no longer is in Interrupted State.
Now then a valid question aries if due to catching of InterruptedException the state is removed why in the world tm.isInterrupted() in main yields true?
Ummmmmm. Scratch ones head and then..... hey its easy.......
You see the Thread-0 (for which all the noise is for), not the main one, was sleeping and when you interrupted it; who says it ran immediately.... It has to go to the ready state, be scheduled and then run..Yes... So meanwhile the main thread executes tm.isInterrupted() as thread has yet to catch(IE) that thread is in interrupted state the main's tm.isInterrupted() answeres true, the Thread-0 catches IE clears itself of the interrupted state and then when you say isInterrupted() in Thread-0; there is no way it can say true, as no longer it is in interrupted state.
Now you wanna verify this theory.....
Just insert
try{
sleep(1000);
}catch(InterruptedException e){
}
after you call tm.interrupt() in main, before the main's tm.isInterrupted(). Without doubt you'll reach the same conclusion as above........


 
sino
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class InterruptSelf extends Thread
{
public void run(){

try{
for(int i=1; i<5; i++){<br /> System.out.print( i + " ");<br /> if(i>2) interrupt(); //self interrupt, will throw an InterruptedException !!!???
Thread.currentThread().sleep(1000);
if( interrupted() ) break;


}//for
}catch(InterruptedException e){
System.out.println("caught it.");
}//


}//
public static void main(String[] args)
{
InterruptSelf th= new InterruptSelf( );
th.start();
}//
}//InterruptSelf

//the result is 123caught it.
//but I really doubt the reason why,
//if I replace the whole run block with the following :
for(int i=1; i<7; i++){<br /> System.out.print( i + " ");<br /> if(i>2) interrupt(); //self interrupt
try{ Thread.currentThread().sleep(1000); }catch(Exception e){}
if( interrupted() ) break;
//if( isInterrupted() ) break;
}//for
//////what will happen now,
//if you take off the sleep(), you will find strange result.
//why???
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'sino',
PROPER NAMES ARE NOW REQUIRED!!
Please Read the JavaRanch naming policy for more details.
Javaranch appreciates your cooperation to comply with the official naming policy.
Ajith
 
Amond Adams
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your new code performs as expected, perhaps you missed the point that interrupt(), interrupted and loosely isInterrupted() are all about internal thread flags, native implementations in C.
When you say interrupt() the thread, you will not kick its state with brute force, all you do is that you set a flag in the internal mechanism of that thread to interrupt. Now if that thread is in sleep or wait or join pool it will try to awake up while throwing an IOException, however if it i'snt in its sleepish mood nothing will happen as it is already in the running or ready state. Now point to remember is that the flag once set to interrupt will stay as flaged strictly unless one of the following happen:
interrupted() is called, the thread itself dies, or IOException is caught. Which means that if you executed interrupt() in running state and then if none of the above occured and that the thread went into sleep/wait/join it is gona get interrupt the second it enters into its sleepish mood, because the flag to interrupt was already set...... Thats what is with your code above........
Simply to understand it better
shuffle you run code as follows:
try{
for(int i=1; i<5; i++){<br /> System.out.print( i + " ");<br /> if(i>2) interrupt();
System.out.println(interrupted());
Thread.currentThread().sleep(1000);
}
}catch(InterruptedException e){
System.out.println("IOE");
}
and then change again as follows:
try{
for(int i=1; i<5; i++){<br /> System.out.print( i + " ");<br /> if(i>2) interrupt();
System.out.println(isInterrupted());
Thread.currentThread().sleep(1000);
}
}catch(InterruptedException e){
System.out.println("IOE");
}
Try and learn well.......
Also it will be polite and humane for one to atleast thank another for his or her efforts....

[This message has been edited by Amond Adams (edited December 04, 2000).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic