A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

白萝卜

初级黑马

  • 黑马币:8

  • 帖子:13

  • 精华:0

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之前不要输出,也不要返回,已经控制了输出,返回语句怎么写啊。。。

2 个回复

倒序浏览
自己顶,不要沉啊
回复 使用道具 举报
不要沉不要沉啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马