日期:2014-05-20 浏览次数:20874 次
public class Thread2 {
public static void main(String[] args) {
Join join = new Join();
Thread runJoin = new Thread(join) ;
runJoin.start();
int i = 0 ;
while(true){
if( i%5 == 0 )
try{
runJoin.join(3000);
}catch(Exception err){err.printStackTrace();}
if( i == 51 )
break ;
System.out.println("hello,i am"+Thread.currentThread().getName()+",now i = "+i);
i++ ;
}
}
}
class Join implements Runnable {
@Override
public void run(){
for( int i=0 ; true ; i++ ){
if( i == 51 )
Thread.currentThread().interrupt();
if( i == 33 )
Thread.currentThread().yield();
System.out.println("现在是我,"+Thread.currentThread().getName()+"在运行,此时 i="+i);
if( Thread.currentThread().isInterrupted() )
System.out.println(Thread.currentThread().getName()+": 额,我被中断了一下下");
try{
Thread.sleep(500);
}
catch(Exception err){err.printStackTrace();}
}
}
Join(){
}
}