思路分析:
线程执行顺序必须是A线程执行、B线程执行、C线程执行
A和B线程,在B线程中调用A.join()方法
B和C线程,在C线程中调用B.
package com.ww.Thread.CreateThread;
public class ABCThread extends Thread{
private String name;
private Thread thread;
public ABCThread(Thread thread, String name) {
this.name = name;
this.thread = thread;
}
@Override
public void run() {
try {
//控制当前线程晚于thread子线程
if(thread!=null){
thread.join();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name);
}
public static void main(String[] args) {
ABCThread a = new ABCThread(null, "A");
ABCThread b = new ABCThread(a, "B"); //在b线程中a.join
ABCThread c = new ABCThread(b, "C");//在c线程中b.join
a.start();
b.start();
c.start();
}
}
join()方法