面试题答案
一键面试-
Condition概述:
- 在Java中,
Condition
是在java.util.concurrent.locks
包下,配合Lock
接口使用,用于实现更灵活的线程间通信。Condition
可以实现类似传统Object
的wait
、notify
和notifyAll
方法的功能,但提供了更细粒度的控制。 Lock
替代了synchronized
关键字,Condition
通过Lock
的newCondition()
方法创建。
- 在Java中,
-
生产者 - 消费者模型代码示例:
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class ProducerConsumer {
private final Queue<Integer> queue;
private final int capacity;
private final Lock lock = new ReentrantLock();
private final Condition notFull = lock.newCondition();
private final Condition notEmpty = lock.newCondition();
public ProducerConsumer(int capacity) {
this.queue = new LinkedList<>();
this.capacity = capacity;
}
public void produce(int item) throws InterruptedException {
lock.lock();
try {
while (queue.size() == capacity) {
notFull.await();
}
queue.add(item);
System.out.println("Produced: " + item);
notEmpty.signal();
} finally {
lock.unlock();
}
}
public int consume() throws InterruptedException {
lock.lock();
try {
while (queue.isEmpty()) {
notEmpty.await();
}
int item = queue.poll();
System.out.println("Consumed: " + item);
notFull.signal();
return item;
} finally {
lock.unlock();
}
}
}
class Producer implements Runnable {
private final ProducerConsumer pc;
private final int id;
public Producer(ProducerConsumer pc, int id) {
this.pc = pc;
this.id = id;
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
pc.produce(i * id);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
class Consumer implements Runnable {
private final ProducerConsumer pc;
private final int id;
public Consumer(ProducerConsumer pc, int id) {
this.pc = pc;
this.id = id;
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
pc.consume();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
public class Main {
public static void main(String[] args) {
ProducerConsumer pc = new ProducerConsumer(2);
Thread producerThread1 = new Thread(new Producer(pc, 1));
Thread producerThread2 = new Thread(new Producer(pc, 2));
Thread consumerThread = new Thread(new Consumer(pc, 1));
producerThread1.start();
producerThread2.start();
consumerThread.start();
try {
producerThread1.join();
producerThread2.join();
consumerThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
在上述代码中:
ProducerConsumer
类封装了共享队列和相关的Lock
与Condition
。produce
方法中,当队列满时,生产者线程调用notFull.await()
进入等待状态,当队列有空间时被唤醒并添加元素,然后调用notEmpty.signal()
唤醒可能等待的消费者线程。consume
方法中,当队列为空时,消费者线程调用notEmpty.await()
进入等待状态,当队列有元素时被唤醒并取出元素,然后调用notFull.signal()
唤醒可能等待的生产者线程。Producer
和Consumer
类分别实现了Runnable
接口,用于模拟生产者和消费者线程的行为。- 在
main
方法中启动多个生产者和消费者线程来测试生产者 - 消费者模型。