class MyThreadExec extends Thread {
private int time; // 保存线程的休眠时间
public MyThreadExec(String name, int time) {
super(name); // 设置线程名称
this.time = time;// 设置休眠的时间
}
public void run() { // 覆写run()方法
try {
Thread.sleep(this.time); // 指定休眠的时间
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "线程, 休眠"
+ this.time + "毫秒");
}
}
public class ThreadExec {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyThreadExec mt1 = new MyThreadExec("线程A", 10000);
// 定义线程对象,指定休眠时间
MyThreadExec mt2 = new MyThreadExec("线程B", 10000);
// 定义线程对象,指定休眠时间
MyThreadExec mt3 = new MyThreadExec("线程C", 10000);
// 定义线程对象,指定休眠时间
mt1.start();
mt2.start();
mt3.start();
}
}