简介
阻塞队列(BlockingQueue)是一个支持两个附加操作的队列。这两个附加的操作是:
在队列为空时,获取元素的线程会等待队列变为非空。
当队列满时,存储元素的线程会等待队列可用。
阻塞队列常用于生产者和消费者的场景,生产者是往队列里添加元素的线程,消费者是从队列里拿元素的线程。阻塞队列就是生产者存放元素的容 器,而消费者也只从容器里拿元素。
BlockingQueue即阻塞队列,从阻塞这个词可以看出,在某些情况下对阻塞队列的访问可能会造成阻塞。被阻塞的情况主要有如下两种:
- 当队列满了的时候进行入队列操作
- 当队列空了的时候进行出队列操作
因此,当一个线程试图对一个已经满了的队列进行入队列操作时,它将会被阻塞,除非有另一个线程做了出队列操作;同样,当一个线程试图对一个空队列进行出队列操作时,它将会被阻塞,除非有另一个线程进行了入队列操作。
实现子类
-
ArrayBlockingQueue:是一个有边界的阻塞队列,它的内部实现是一个数组。有边界的意思是它的容量是有限的,我们必须在其初始化的时候指定它的容量大小,容量大小一旦指定就不可改变。
-
LinkedBlockingQueue:LinkedBlockingQueue阻塞队列大小的配置是可选的,如果我们初始化时指定一个大小,它就是有边界的,如果不指定,它就是无边界的。说是无边界,其实是采用了默认大小为 Integer.MAX_VALUE 的容量 。它的内部实现是一个链表。
-
PriorityBlockingQueue:PriorityBlockingQueue是一个没有边界的队列,它的排序规则和 java.util.PriorityQueue一样。需要注意,PriorityBlockingQueue中允许插入null对象。所有插入PriorityBlockingQueue的对象必须实现 java.lang.Comparable接口,队列优先级的排序规则就按照我们对这个接口的实现来定义的。另外,我们可以从PriorityBlockingQueue获得一个迭代器Iterator,但这个迭代器并不保证按照优先级顺序进行迭代。
-
SynchronousQueue:SynchronousQueue队列内部仅允许容纳一个元素。当一个线程插入一个元素后会被阻塞,除非这个元素被另一个线程消费。
生产者与消费者例子:
使用
public class ThreadQueue {
public static void main(String[] args) {
BlockingQueue<String> queue = new ArrayBlockingQueue<>(3);//最多只能存3个
queue.offer("张三");
queue.offer("李四");
queue.offer("王五");
queue.offer("赵六");
System.out.println(queue.size());
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll());
}
}
生产者消费者例子
class ProducerThread implements Runnable {
private BlockingQueue queue;
private volatile boolean flag = true;
private static AtomicInteger count = new AtomicInteger();
public ProducerThread(BlockingQueue queue) {
this.queue = queue;
}
@Override
public void run() {
try {
System.out.println("生产线程启动...");
while (flag) {
System.out.println("正在生产数据....");
String data = count.incrementAndGet()+"";
// 将数据存入队列中
boolean offer = queue.offer(data, 2, TimeUnit.SECONDS);
if (offer) {
System.out.println("生产者,存入" + data + "到队列中,成功.");
} else {
System.out.println("生产者,存入" + data + "到队列中,失败.");
}
Thread.sleep(1000);
}
} catch (Exception e) {
} finally {
System.out.println("生产者退出线程");
}
}
public void stop() {
this.flag = false;
}
}
class ConsumerThread implements Runnable {
private BlockingQueue<String> queue;
private volatile boolean flag = true;
public ConsumerThread(BlockingQueue<String> queue) {
this.queue = queue;
}
@Override
public void run() {
System.out.println("消费线程启动...");
try {
while (flag) {
System.out.println("消费者,正在从队列中获取数据..");
String data = queue.poll(2, TimeUnit.SECONDS);
if (data != null) {
System.out.println("消费者,拿到队列中的数据data:" + data);
Thread.sleep(1000);
} else {
System.out.println("消费者,超过2秒未获取到数据..");
flag = false;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("消费者退出线程...");
}
}
}
public class QueueProducerAndConsumer {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<String> queue = new LinkedBlockingQueue<String>(10);
ProducerThread producerThread1 = new ProducerThread(queue);
ProducerThread producerThread2 = new ProducerThread(queue);
ConsumerThread consumerThread1 = new ConsumerThread(queue);
Thread t1 = new Thread(producerThread1);
Thread t2 = new Thread(producerThread2);
Thread c1 = new Thread(consumerThread1);
t1.start();
t2.start();
c1.start();
// 执行10s
Thread.sleep(10 * 1000);
producerThread1.stop();
producerThread2.stop();
}
}
实现定时器
任务调度配合Callable,达到定时器的效果
ScheduledExecutorService newScheduledThreadPool = Executors.newScheduledThreadPool(5);
for (int i = 0; i < 10; i++) {
final int tmp = i;
ScheduledFuture<Integer> schedule = newScheduledThreadPool.schedule(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return new Random().nextInt(100);
}
},3,TimeUnit.SECONDS);
System.out.println(schedule.get());//每隔3秒执行一个线程
}