您的购物车目前是空的!
测试新插件
测试enlighter插件
我需要一段代码
import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; public class ProducerConsumer { private final ReentrantLock lock = new ReentrantLock(); private final Condition notFull = lock.newCondition(); private final Condition notEmpty = lock.newCondition(); private final int[] buffer = new int[10]; private int count = 0; private int putIndex = 0; private int takeIndex = 0; public void produce(int value) { lock.lock(); try { while (count == buffer.length) { notFull.await(); } buffer[putIndex] = value; putIndex = (putIndex + 1) % buffer.length; count++; notEmpty.signal(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } public int consume() { lock.lock(); try { while (count == 0) { notEmpty.await(); } int value = buffer[takeIndex]; takeIndex = (takeIndex + 1) % buffer.length; count--; notFull.signal(); return value; } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } return -1; } }