在项目中会有线程顺序执行的一些需求,比如...,我要吃早餐,但吃早餐必须要等女朋友做好早餐才能吃,但是在等着的同时我可以做其他的事情,比如看电视,我们写个小demo来看一下:
import java.text.SimpleDateFormat;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 线程顺序执行
*
* @author feng
* @create 2017-09-11 17:35
**/
public class ThreadJoinTest {
static final String STR ="HH:mm:ss";
static final SimpleDateFormat dateFormat = new SimpleDateFormat(STR);
//启动3个线程并保证执行顺序为A、B、C
public static void main(String[] args) throws InterruptedException {
final Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + ":做饭 " + dateFormat.format(System.currentTimeMillis()));
Thread.sleep(5000);
System.out.println(Thread.currentThread().getName() + ":做好饭了 " + dateFormat.format(System.currentTimeMillis()));
} catch (InterruptedException e) {
}
}
},"女朋友");
final Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {