相比于 wait() 这种等待方式,await() 可以指定等待的条件,举个代码例子
时间: 2023-05-26 12:03:28 浏览: 55
Java 中的 await() 和 notify() 方法需要在使用前使用 synchronized 关键字进行同步。
例如,以下是一个使用 wait() 和 notify() 方法的例子:
```java
public class WaitNotifyExample {
public static void main(String[] args) throws InterruptedException {
Message message = new Message("Important message");
Thread sender = new Thread(new Sender(message));
Thread receiver = new Thread(new Receiver(message));
sender.start();
receiver.start();
sender.join();
receiver.join();
}
}
class Sender implements Runnable {
private Message message;
public Sender(Message message) {
this.message = message;
}
public void run() {
String names[] = {"Alice", "Bob", "Charlie"};
for (String name : names) {
message.send(name);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
message.send("Done");
}
}
class Receiver implements Runnable {
private Message message;
public Receiver(Message message) {
this.message = message;
}
public void run() {
String msg = "";
while (!msg.equals("Done")) {
msg = message.receive();
System.out.println("Received: " + msg);
}
}
}
class Message {
private String message;
private boolean empty = true;
public synchronized String receive() {
while (empty) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
empty = true;
notifyAll();
return message;
}
public synchronized void send(String message) {
while (!empty) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
empty = false;
this.message = message;
notifyAll();
}
}
```
这里创建了两个线程,一个线程将一个重要消息发送给多个不同的接收者,另一个线程接收该消息并处理它。在接收程序中,我们用 while 循环等待消息,直到它不再为空。在发送程序中,我们等待直到消息不再被占用,然后将消息发送给接收程序。
相比之下,使用 await() 和 notify() 方法的代码会更加简洁:
```java
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class AwaitNotifyExample {
public static void main(String[] args) throws InterruptedException {
Message message = new Message("Important message");
Thread sender = new Thread(new Sender(message));
Thread receiver = new Thread(new Receiver(message));
sender.start();
receiver.start();
sender.join();
receiver.join();
}
}
class Message {
private String message;
private boolean empty = true;
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public Message(String message) {
this.message = message;
}
public void send(String message) throws InterruptedException {
lock.lock();
try {
while (!empty) {
condition.await();
}
empty = false;
this.message = message;
condition.signalAll();
} finally {
lock.unlock();
}
}
public String receive() throws InterruptedException {
lock.lock();
try {
while (empty) {
condition.await();
}
empty = true;
condition.signalAll();
return message;
} finally {
lock.unlock();
}
}
}
class Sender implements Runnable {
private Message message;
public Sender(Message message) {
this.message = message;
}
public void run() {
String names[] = {"Alice", "Bob", "Charlie"};
for (String name : names) {
try {
message.send(name);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
message.send("Done");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Receiver implements Runnable {
private Message message;
public Receiver(Message message) {
this.message = message;
}
public void run() {
String msg = "";
try {
while (!msg.equals("Done")) {
msg = message.receive();
System.out.println("Received: " + msg);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
```
使用 await() 或 signal() 来等待或通知线程,代替了使用 wait() 和 notify()。通过更加清晰的代码,我们创建了一个与前一个示例类似的线程协调程序。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)