package runnableTest;
public class ThreadDemo extends Thread{
private Thread t;
private String threadName;
public ThreadDemo(String name) {
threadName = name;
System.out.println("第一步:createing:" + name);
}
public void run() {
System.out.println("第三步:runing:" + threadName);
try {
for (int i = 5;i > 0; i--) {
System.out.println("Thread:" + threadName + ", " + i);
if (threadName.equals("线程名称二")) {
Thread.sleep(5000);
}
}
}catch (InterruptedException e){
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread " + threadName + " exiting.");
}
public void start() {
System.out.println("第二步:starting:" + threadName);
if (t == null) {
t = new Thread(this, threadName);
t.start();
}
}
}
调用这个类
package runnableTest;
public class TestThread2 {
public static void main(String[] args){
ThreadDemo T1 = new ThreadDemo("线程名称一");
T1.start();
ThreadDemo T2 = new ThreadDemo("线程名称二");
T2.start();
}
}
|