程序如下:
class ThreadDemo1 implements Runnable{//实现Runnable接口
private String name;
private int time;
public ThreadDemo1(String name,int time){
this.name=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(this.name+"线程休眠"+this.time);
}
}
public class MyThread {
public static void main(String[] args) {
ThreadDemo1 thread01=new ThreadDemo1("A",1000);//实现Runnable 子类对象;
ThreadDemo1 thread02=new ThreadDemo1("B",2000);
ThreadDemo1 thread03=new ThreadDemo1("C",3000);
new Thread(thread01).start();//实例化Thread类,启动线程
new Thread(thread02).start();
new Thread(thread03).start();
}
}
运行如下:
A线程休眠1000
B线程休眠2000
C线程休眠3000