停止线程方法.

import java.awt.*;
import java.util.Date;

import javax.swing.*;
public class Test {

public static void main(String[]args){
myRunnable my =new myRunnable();
Thread myThread= new Thread(my);
myThread.start();

try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
}
my.shutDown();//停止线程方法
System.out.println("主方法线程");

}
}
class myRunnable implements Runnable {
private boolean bool = true;
public void run() {
//while(true){
for(int i =0;i<10;++i){
System.out.println(new Date());// TODO Auto-generated method stub
try {
Thread.sleep(1000);
}catch(InterruptedException e){
}
}
}
public void shutDown(){//用于停止线程
bool = false;

}
}

你可能感兴趣的:(线程)