class test
{
public static void main(String[] args) {
Thread th=new Thread()
{
public void run(){
for (int i = 1; i <= 18; i++) {
System.out.println("run:"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
}
}
}};
Thread y=new Thread()
{
public void run() {
for (int i = 1; i < 10; i++) {
System.out.println("stop:"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
}
}
}
};
th.start();
y.start();
for (int i = 0; i < 5; i++) {
System.out.println("main:"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
}
}
System.out.println("main completed");
}
}
这里我想在线程y完成执行后立即停止线程,我该怎么做?如何在其他完成后停止一个线程?
您可以让一个线程中断另一个线程。 –