阻塞队列生产者消费
作者:互联网
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();
}
}
}
}
}
标签:BlockOne,队列,void,阻塞,private,生产者,new,arrayBlockingQueue,public 来源: https://www.cnblogs.com/limingming1993/p/15043315.html