结论
synchronized
synchronized 共同 wait()/notify 无法实现精准唤醒线程
ReentrantLock
ReentrantLock 共同 Condition await()/signal() 可以实现精准唤醒线程 (指唤醒指定的线程)
ReentrantLock 怎样实现精准唤醒线程
一个 lock 共同多个 Condition, 且每个 Condition 中只有一个线程
(若一个Condition中有多个线程,也无法精准唤醒线程)
案例 synchronized
- public class ManySync {
- public void await1() {
- synchronized (this) {
- try {
- System.out.println("await1 - 进入等待" + Thread.currentThread().getName());
- this.wait();
- System.out.println("await1 - 被唤醒" + Thread.currentThread().getName());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public synchronized void await2() {
- synchronized (this) {
- try {
- System.out.println("await2 - 进入等待" + Thread.currentThread().getName());
- this.wait();
- System.out.println("await2 - 被唤醒" + Thread.currentThread().getName());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public void await3() {
- synchronized (this) {
- try {
- System.out.println("await3 - 进入等待" + Thread.currentThread().getName());
- this.wait();
- System.out.println("await3 - 被唤醒" + Thread.currentThread().getName());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public void await4() {
- synchronized (this) {
- try {
- System.out.println("await4 - 进入等待" + Thread.currentThread().getName());
- this.wait();
- System.out.println("await4 - 被唤醒" + Thread.currentThread().getName());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public void notif() {
- synchronized(this) {
- try {
- this.notify();
- this.notify();
- this.notify();
- this.notify();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- class Run3 {
- public static void main(String[] args) {
- try {
- ManySync manySync = new ManySync();
- Thread t1 = new Thread(() -> {
- manySync.await1();
- });
- Thread t2 = new Thread(() -> {
- manySync.await2();
- });
- Thread t3 = new Thread(() -> {
- manySync.await3();
- });
- Thread t4 = new Thread(() -> {
- manySync.await4();
- });
- t1.start();
- Thread.sleep(200);
- t2.start();
- Thread.sleep(200);
- t3.start();
- Thread.sleep(200);
- t4.start();
- Thread.sleep(1000);
- manySync.notif();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
复制代码 输出结果 可以看出等待时间长的锁并不会开始被唤醒, 也无法唤醒指定线程,(因为并未提供能唤醒的API,也无法进行设计) 唤醒的输出结果是随机的 由 JVM 调理
- await1 - 进入等待Thread-0
- await2 - 进入等待Thread-1
- await3 - 进入等待Thread-2
- await4 - 进入等待Thread-3
- await1 - 被唤醒Thread-0
- await4 - 被唤醒Thread-3
- await3 - 被唤醒Thread-2
- await2 - 被唤醒Thread-1
复制代码 案例 ReentrantLock
输出结果 这里可以看出 ,当每个Condition 中只有一个线程时, 可以通过逻辑控制, 实现精准唤醒必要唤醒的线程
- await1 - 进入等待Thread-0
- await2 - 进入等待Thread-1
- await3 - 进入等待Thread-2
- await4 - 进入等待Thread-3
- await1 - 被唤醒 - main
- await2 - 被唤醒 - main
- await3 - 被唤醒 - main
- await4 - 被唤醒 - main
- await1 - 被唤醒Thread-0
- await2 - 被唤醒Thread-1
- await3 - 被唤醒Thread-2
- await4 - 被唤醒Thread-3
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |