Thread的wait和notify是指的是 Runnable对象而不是 Thread对象,切记,否则唤醒失效

本文探讨了Java中线程间的同步与通信机制,通过实现`TestThreadNotifyWait`类,展示了如何利用`synchronized`关键字和`notify()`、`wait()`方法来协调多个线程的执行流程,实现高效并发操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.wanju.project001.zonghe.test;


public class TestThreadNotifyWait {


public static void main(String[] args) {
T1 rt1=new T1();
Thread t1 = new Thread(rt1);
Thread t2 = new Thread(new T2(t1));
t1.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// t2.start();
synchronized (rt1) {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "----"
+ i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
rt1.notify();
}
// int i=0;
// synchronized (rt1) {
// while (++i <= 10) {
// try {
// Thread.sleep(500);
// System.out.println("The main thread.-->" + i);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// rt1.notify();
// }

}
}


class T1 implements Runnable {
public void run() {
// synchronized (this) {
// for (int i = 0; i < 10; i++) {
// if (i == 4) {
//
// try {
// this.wait();
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// // this.notifyAll();
// }
// System.out.println(Thread.currentThread().getName() + "----"
// + i);
// try {
// Thread.sleep(500);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// }

int i = 0;
// 使用对象本身this作为监视器(与主线程的监视器为同一个对象)
synchronized (this) {
while (i++ < 20) {
try {
Thread.sleep(500);
System.out.println("The sub thread.-->" + i);
if (i == 10) {
// 释放监视器锁,阻塞等待...
this.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}


}
}


class T2 implements Runnable {
Thread t1;


public T2() {
// TODO Auto-generated constructor stub
}


public T2(Thread t1) {
this.t1 = t1;
}


public void run() {
synchronized (t1) {


for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "----"
+ i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
t1.notify();
}
}
}
### Java 中 `notify` 唤醒等待池中的第一个线程 在多线程编程中,`Object.notify()` 方法用于唤醒正在调用对象监视器上等待的一个线程。当多个线程处于等待状态时,JVM会选择其中一个线程并将其标记为可运行。需要注意的是,具体哪个线程被选中是由 JVM 决定的,并不是按照先进先出的原则来选择。 下面是一个简单的例子展示如何使用 `wait()` `notify()` 来实现线程间的通信: ```java public class NotifyExample { private static final Object lock = new Object(); public static void main(String[] args) throws InterruptedException { Thread waitingThread = new Thread(() -> { synchronized (lock) { try { System.out.println(Thread.currentThread().getName() + " is waiting."); lock.wait(); // Wait until notified. System.out.println(Thread.currentThread().getName() + " has been woken up."); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } }); Thread notifyingThread = new Thread(() -> { synchronized (lock) { System.out.println(Thread.currentThread().getName() + " is about to notify."); lock.notify(); // Wake up one of the threads that are waiting on this object's monitor. System.out.println(Thread.currentThread().getName() + " has notified."); } }); waitingThread.start(); Thread.sleep(100); // Ensure the waiting thread starts before the notifying thread. notifyingThread.start(); waitingThread.join(); notifyingThread.join(); } } ``` 在这个程序里,两个线程共享同一个锁对象 (`lock`)。`waitingThread` 调用了 `wait()` 进入等待状态,直到另一个线程通过调用 `notifyingThread` 的 `notify()` 将其唤醒为止。值得注意的是,在实际应用中应当避免长时间阻塞主线程,这里只是为了演示目的才这样做[^1]。 对于更复杂的场景,建议考虑使用更高层次并发工具如 `CountDownLatch`, `CyclicBarrier`, 或者基于 `Future` 接口的任务提交机制等替代原始的 `wait/notify` 组合,因为这些高级API提供了更好的抽象级别更高的安全性[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静山晚风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值