package queue;

import sun.awt.CustomCursor;

import java.util.concurrent.ArrayBlockingQueue;
// 初始生产者和消费者
public class BlockQuery {

public static void main(String[] args) {
BlockQuery blockQuery = new BlockQuery();

BlockOne blockOne = blockQuery.new BlockOne();
Customer customCursor = blockQuery.new Customer();
blockOne.start();
customCursor.start();

}

private int queryInt = 10;
private ArrayBlockingQueue<Integer> arrayBlockingQueue = new ArrayBlockingQueue<Integer>(queryInt);

// 使用内部类 简单制作
class BlockOne extends Thread {
@Override
public void run() {
BlockOne();
}

// 生产者
private void BlockOne() {
while (true) {
try {
arrayBlockingQueue.put(1);
System.out.println("一直再生产,从未被放弃" + (queryInt - arrayBlockingQueue.size()));
} catch (InterruptedException e) {
e.printStackTrace();
}

}
}
}

// 消费者简单
class Customer extends Thread {
@Override
public void run() {
customer();

}

private void customer() {
while (true) {
try {
arrayBlockingQueue.take();
System.out.println("来啊,一直在消费" + (arrayBlockingQueue.size()));
} catch (InterruptedException e) {
e.printStackTrace();
}

}

}


}


}
posted on 2021-07-22 11:30  沐雨清晨  阅读(32)  评论(0)    收藏  举报