package com.hao.javabasis.test.thread;
import java.util.Objects;
/**
* 继承自 Thread
*/
public class ThreadTwo extends Thread {
private Thread thread;
private String name;
public ThreadTwo(String threadName) {
name = threadName;
}
public void run() {
System.out.println("线程开始启动" + name);
try {
for (int i = 0; i < 4; i++) {
System.out.println("Thread: " + name + ", " + i);
Thread.sleep(50); //线程休眠
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程退出" + name);
}
public void start() {
System.out.println("Starting" + name);
if (Objects.isNull(thread)) {
thread = new Thread(this, name);
thread.start();
}
}
}
class TestThreadTwo {
public static void main(String[] args) {
ThreadTwo one = new ThreadTwo("thread-1");
one.start();
ThreadTwo two = new ThreadTwo("thread-2");
two.start();
}
} package com.hao.javabasis.test.thread;