主题:[讨论]怎么终止一个线程?
class T1{
public static void main(String[] d)throws Exception
{
Thread t1=new Thread(new T2());
Thread t2=new Thread(new T2());
Thread t3=new Thread(new T2());
t1.start();
t1.interrupt();
t2.start();
t3.start();
System.out.println(t1.isAlive()+" "+t2.isAlive()+t3.isAlive());
}
}
class T2 implements Runnable{
public T2()
{
}
public void run()
{
for(double a=0.00001;a<0.0001;a+=0.00001)
{
System.out.println(Thread.currentThread());
System.out.println(Thread.currentThread());
System.out.println(Thread.currentThread());
System.out.println(Thread.currentThread());
try{
Thread.currentThread().interrupt();
}catch(Exception e)
{
}
}
}
}
按理说在main()中执行完t1.interrupt()之后,t1就不应该再执行了,但为什么控制台中在t2.start()之后依然有有关t1线程的输出呢?
public static void main(String[] d)throws Exception
{
Thread t1=new Thread(new T2());
Thread t2=new Thread(new T2());
Thread t3=new Thread(new T2());
t1.start();
t1.interrupt();
t2.start();
t3.start();
System.out.println(t1.isAlive()+" "+t2.isAlive()+t3.isAlive());
}
}
class T2 implements Runnable{
public T2()
{
}
public void run()
{
for(double a=0.00001;a<0.0001;a+=0.00001)
{
System.out.println(Thread.currentThread());
System.out.println(Thread.currentThread());
System.out.println(Thread.currentThread());
System.out.println(Thread.currentThread());
try{
Thread.currentThread().interrupt();
}catch(Exception e)
{
}
}
}
}
按理说在main()中执行完t1.interrupt()之后,t1就不应该再执行了,但为什么控制台中在t2.start()之后依然有有关t1线程的输出呢?