提问生产者消费者的代码题
package f;import java.util.List;public class A implements Runnable { private final List<Data> queue; public A(List<Data> queue) { this.queue = queue; }//消费者@Overridepublic void run(){ try { while (true) { synchronized (queue) { if((1)){ //1 (2) //2 queue.notifyAll(); } Data data = queue.remove(0); System.out.println(data.getData()); } Thread.sleep(1000); } } catch (InterruptedException e){ e. printStackTrace(); } }}package f;import java.util.List;public class B implements Runnable { private final List<Data> queue; private final int length; //缓存最大值public B(List<Data> queue,int length) { this.queue=queue; this.length = length; }@Override public void run() { try{ while (true) { synchronized (queue) { if( (3) ){ //3 (4) //4 (5) //5 } else{ Data data = new Data(); queue.add(data); } } Thread.sleep(1000); } } catch (InterruptedException e) { e. printStackTrace(); } } }
我觉得题应该是出错了,消费者的if后面是不是少了个else,否则不加判断的就直接remove吗#微博#
我觉得题应该是出错了,消费者的if后面是不是少了个else,否则不加判断的就直接remove吗#微博#