【简答题】![](https://cos-cdn.shuashuati.com/shuashuati-wap/asset/rightlogo.png)
本题程序中实现了一个“生产者一消费者问题”。生产者产生一个随机数存入DataPool类中,消费者从中取出数据。DataPool类一次只能存放一个数据。请更正题中带下划线的部分。 注意:不改变程序的结构,不得增行或删行。 class DataPool { private int data; private boolean isFull; public DataPool() { isFull=false; } public synchronized void putData(int d) { if(isFull= =true) { try { this.notify(); } catch(InterruptedException e) {} } data=d; isFull=true; System.out.println("生产了一个数据:"+data); this.notify(); } public synchronized int getData() { if(isFull= =false) { try { this.wait(); } catch(InterruptedException e) {} } isFull=false; System.out.println("消费了一个数据"+data); this.wait(); return this.data; } boolean getIsFull() { return isFull; } } class Producer extends Thread { DataPool pool; public Producer(DataPool pool) { this.pool=pool; } public void run() { for(int i=0; i<10; i++) { int data=(int) (Math.random()*1000); try {//用于生产数据 sleep(data); } catch(InterruptedException e) {} pool.putData(data); } } } class Consumer implements Runnable { DataPool pool; public Consumer(DataPool pool) { this.pool=pool; } public void run() { for(int i=0; i<10; i++) { int data=pool.getData(); try {//用于处理数据 sleep((int) (Math.random()*1000)); } catch(InterruptedException e) {} } } } public class advance } public static void main(String[] args) { Data Pool pool=new Data Pool(); Producer pro=new Producer(pool); Runnable con=new Consumer(pool); Thread conTh=new Thread(con); pro.start(); conTh.start(); } }
举报
参考答案:
![](https://cos-cdn.shuashuati.com/shuashuati-wap/asset/rightlogo.png)
参考解析:
![](https://cos-cdn.shuashuati.com/shuashuati-wap/asset/rightlogo.png)
举一反三
![](https://cos-cdn.shuashuati.com/shuashuati-wap/asset/rightlogo.png)