生活案例
我想泡茶,需要洗水壶,烧开水,洗茶壶,洗茶杯,那茶叶。
利用多线程操作:
线程一(老王):洗水壶1分钟,烧开水15分钟
线程二(小赵):洗茶壶,洗茶杯,拿茶叶4分钟
public class Tea {
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
System.out.println("洗水壶");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("烧开水");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "老王");
Thread t2 = new Thread(() ->{
System.out.println("洗茶壶");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {