sino

Greenhorn
+ Follow
since Nov 29, 2000
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by sino

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???
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???
if a class extends Thread and override the run(), but when instance it, pass in another Runnable target,
then whose run() method will this class object use??

class ThreadRun extends Thread{
ThreadRun(RunTarget r){ super(r); }
public void run(){ System.out.println("origianl. "); }
}//
class RunTarget implements Runnable{
public void run(){
System.out.println("RunTarget's run: ");
}//
}//
public class Test{
public static void main(String[] args)
{

ThreadRun th= new ThreadRun (new RunTarget() );
th.start();
}//main
}//
what will be this code's result?
and WHY??
Hi All,
class A {
int i = 10;
class A1 { //inner member class
int i = 20;
}
}

class B {
int j = 30;
B(int i, int j) {
//Here I want to initialize to i variable of //A1 class. Is this possible?
this.j = j;
}
}//

Thanks.
what about if there is no inheritance between class A and B, and I still want to do the same thing in B??
I have tried:
A a = new A();
A.A1 a1= a.new A.A1;
but doesn't work!!!