老师您好!请问:我这个生产者已经完成生产为什么程序没有结束。是不是消费者消费完了接着通知生产者生产了?但是这个生产者具体是在哪里卡着了呢?这个应该怎样修改。
public class BlockingQueueLYY {
public static void main(String[] args) throws InterruptedException {
ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(10);
Consumer consumer = new Consumer(arrayBlockingQueue);
Thread consumerT = new Thread(consumer);
Product product = new BlockingQueueLYY().new Product(arrayBlockingQueue,consumerT);
Thread productT = new Thread(product);
productT.start();
Thread.sleep(1000);
consumerT.start();
}
class Product implements Runnable{
private BlockingQueue blockingQueue;
Thread thread;
public Product(BlockingQueue blockingQueue,Thread thread){
this.blockingQueue = blockingQueue;
this.thread = thread;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
blockingQueue.put(i);
System.out.println(i+"被放进去了");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("生产完成");
thread.interrupt();
}
}
static class Consumer implements Runnable{
private BlockingQueue blockingQueue;
Consumer(BlockingQueue blockingQueue){
this.blockingQueue = blockingQueue;
}
@Override
public void run() {
while (1==1){
try {
blockingQueue.take();
System.out.println("消费了");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}