在 Java 中,Object
类提供了wait()
方法,用于线程间的协作和同步。wait()
方法使得当前线程暂停执行,并释放当前对象的锁,直到其他线程调用该对象的notify()
或notifyAll()
方法将其唤醒。这是实现线程间通信和同步的重要机制之一。
wait()
方法的使用
wait()
方法定义在Object
类中,因此所有 Java 对象都继承了这个方法。它有三种重载形式:
• wait()
:使当前线程等待,直到其他线程调用该对象的notify()
或notifyAll()
方法。
• wait(long timeout)
:使当前线程等待,直到其他线程调用该对象的notify()
或notifyAll()
方法,或者指定的超时时间到期。
• wait(long timeout, int nanos)
:使当前线程等待,直到其他线程调用该对象的notify()
或notifyAll()
方法,或者指定的超时时间到期(精确到纳秒)。
使用wait()
和notify()
的注意事项
• 必须在同步块中调用:wait()
和notify()
方法必须在同步块(synchronized
块)中调用,否则会抛出IllegalMonitorStateException
异常。
• 释放锁:调用wait()
方法时,当前线程会释放当前对象的锁。
• 线程中断:如果线程被中断,wait()
方法会抛出InterruptedException
异常。
• 超时机制:如果指定了超时时间,线程会在超时后自动唤醒。
示例代码
以下是一个简单的生产者-消费者示例,展示如何使用wait()
和notify()
方法来实现线程间的同步。
生产者-消费者示例
java
import java.util.LinkedList;
import java.util.Queue;
public class ProducerConsumerExample {
public static void main(String[] args) {
Buffer buffer = new Buffer();
Thread producerThread = new Thread(new Producer(buffer));
Thread consumerThread = new Thread(new Consumer(buffer));
producerThread.start();
consumerThread.start();
}
}
class Buffer {
private Queue<Integer> queue = new LinkedList<>();
private int capacity;
public Buffer(int capacity) {
this.capacity = capacity;
}
public synchronized void put(int value) throws InterruptedException {
while (queue.size() == capacity) {
wait(); // 等待,直到有空间放置新元素
}
queue.add(value);
notifyAll(); // 唤醒等待的消费者线程
}
public synchronized int get() throws InterruptedException {
while (queue.isEmpty()) {
wait(); // 等待,直到有元素可消费
}
int value = queue.poll();
notifyAll(); // 唤醒等待的生产者线程
return value;
}
}
class Producer implements Runnable {
private Buffer buffer;
public Producer(Buffer buffer) {
this.buffer = buffer;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
System.out.println("Produced: " + i);
buffer.put(i);
Thread.sleep((int)(Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable {
private Buffer buffer;
public Consumer(Buffer buffer) {
this.buffer = buffer;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
int value = buffer.get();
System.out.println("Consumed: " + value);
Thread.sleep((int)(Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
代码说明
• Buffer
类:
• 使用synchronized
方法确保线程安全。
• put
方法在队列满时调用wait()
,在添加元素后调用notifyAll()
。
• get
方法在队列空时调用wait()
,在移除元素后调用notifyAll()
。
• Producer
类:
• 生产者线程调用Buffer
的put
方法生产数据。
• Consumer
类:
• 消费者线程调用Buffer
的get
方法消费数据。
使用场景
wait()
和notify()
方法通常用于以下场景:
• 生产者-消费者模式:生产者线程生成数据,消费者线程消费数据。
• 线程间通信:多个线程需要基于某些条件进行协作。
• 资源管理:多个线程共享有限资源,需要同步访问。
总结
wait()
方法是 Java 中实现线程间同步的重要工具之一。它使得线程可以暂停执行并释放锁,直到其他线程通过notify()
或notifyAll()
方法将其唤醒。通过合理使用wait()
和notify()
,可以实现高效的线程间协作和同步。