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完成執行後立即停止線程,我該怎麼做?如何在其他完成後停止一個線程?
您可以讓一個線程中斷另一個線程。 –