public void put(Object x) throws InterruptedException {
if (isok && count == 0) {
isok = false; // 可以第一次执行生产
}
if (!isok) {
lock.lock();
try {
while (count == items.length)
notFull.await();
items[putptr] = x;
if (++putptr == items.length)
putptr = 0;
++count;
System.out.println(Thread.currentThread().getName() + "...生产者5.0..." + count);
if (count == items.length)
notEmpty.signal();
} finally {
lock.unlock();
}
}
}
public Object take() throws InterruptedException {
if (!isok && count == items.length) {
isok = true; // 可以第一次执行消费
}
if (isok) {
lock.lock();
try {
while (count == 0)
notEmpty.await();
Object x = items[takeptr];
if (++takeptr == items.length)
takeptr = 0;
--count;
System.out.println(Thread.currentThread().getName() + "...消费者5.0..." + count);
if (count == 0)
notFull.signal();
} finally {
lock.unlock();
}
}
}
//定义了多线程,一个线程执行输出任务,另一个线程执行的是输入的任务,需求是,在输入5000之前不要输出,也不要返回,已经控制了输出,返回语句怎么写啊。。。
|
|