ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

阻塞队列生产者消费

2021-07-22 11:33:04  阅读:12  来源: 互联网

标签:BlockOne 队列 void 阻塞 private 生产者 new arrayBlockingQueue public


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

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有