华罗庚《统筹方法》,给出烧水泡茶的多线程解决方案,注意用sleep方法模拟任务花费时间:
- 参考图二,用两个线程(两个人协作)模拟烧水泡茶过程
 - 文中办法乙、丙都相当于任务串行
 - 图一相当于启动4个线程
 

解法1:join
public class Tc {public static void main(String[] args) {Thread t1= new Thread(()->{try {System.out.println("洗水壶");Thread.sleep(1);System.out.println("烧开水");Thread.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}},"老王");Thread t2 = new Thread(()->{try {System.out.println("洗茶壶");Thread.sleep(1);System.out.println("洗茶杯");Thread.sleep(2);System.out.println("拿茶叶");Thread.sleep(1);t1.join();//线程同步,等待线程t1执行完毕后再继续执行} catch (InterruptedException e) {e.printStackTrace();}System.out.println("泡茶");},"小王");t1.start();t2.start();}}
线程t2中“拿茶叶”结束后,要和线程t1同步,等待线程t1执行完毕后(水烧开后),再泡茶。
