Java多线程相关知识【4】--线程同步

# Java多线程相关知识【4】--线程同步

菜鸟的一个学习笔记,欢迎大神<font color=red>批评指正</font>。

## 利用join()使多个线程同步

```java
Thread t1 = new Thread(() -> {
try {
Thread.sleep(RANDOM.nextInt(1000));
System.out.println(Thread.currentThread().getName() + " finished!");
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread t2 = new Thread(() -> {
try {
Thread.sleep(RANDOM.nextInt(1000));
System.out.println(Thread.currentThread().getName() + " finished!");
} catch (InterruptedException e) {
e.printStackTrace();
}
});
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("同步后执行");
```


0 个评论

要回复文章请先登录注册