今天我们通过实例来学习一下BlockingQueue的用法。梦想,可以天花乱坠,理想,是我们一步一个脚印踩出来的坎坷道路。

BlockingQueue的实例

官方文档上的对于BlockingQueue的说明:

A Queue that additionally supports operations that wait for the queue to become non-empty when retrieving an element, and wait for space to become available in the queue when storing an element.

如果BlockQueue是空的,从BlockingQueue取东西的操作将会被阻断进入等待状态,直到BlockingQueue进了东西才会被唤醒.同样,如果BlockingQueue是满的,任何试图往里存东西的操作也会被阻断进入等待状态,直到BlockingQueue里有空间才会被唤醒继续操作

一、BlockingQueue的简单使用

package com.linux.huhx.concurreny;

import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit; public class BlockingQueueTest {
public static void main(String[] args) {
BlockingQueue q = new LinkedBlockingDeque();
Producer p = new Producer(q);
Consumer c1 = new Consumer(q);
Consumer c2 = new Consumer(q);
new Thread(p).start();
new Thread(c1).start();
new Thread(c2).start();
} static class Producer implements Runnable {
private final BlockingQueue<String> queue;
Producer(BlockingQueue<String> queue) {
this.queue = queue;
}
@Override
public void run() {
for (int i = 0; i < 4; i++) {
try {
queue.put("producer" + i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} static class Consumer implements Runnable {
private final BlockingQueue queue;
Consumer(BlockingQueue queue) {
this.queue = queue;
}
public void run() {
for (int i = 0; i < 2; i++) {
try {
TimeUnit.MILLISECONDS.sleep(new Random().nextInt(2000));
System.out.println(queue.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}

运行的结果如下:使用put和take方法,打印的结果是固定的。它会阻塞。

producer0
producer1
producer2
producer3

二、ArrayBlockIngQueue的使用

package com.linux.thread.thread;

import org.junit.Test;

import java.util.concurrent.ArrayBlockingQueue;

public class ArrayBlockQueueTest {
@Test
public void put() {
try {
ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(3);
queue.put("huhx");
queue.put("linux");
queue.put("ll");
System.out.println("size: " + queue.size());
System.out.println("begin: " + System.currentTimeMillis());
queue.put("tomhu"); // 阻塞
System.out.println("end: " + System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
} @Test
public void get() {
try {
ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(3);
System.out.println("begin: " + System.currentTimeMillis());
System.out.println(queue.take()); // 阻塞
System.out.println("end: " + System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

友情链接

java高级---->Thread之BlockingQueue的使用的更多相关文章

  1. java高级---->Thread之ScheduledExecutorService的使用

    ScheduledExecutorService的主要作用就是可以将定时任务与线程池功能结合使用.今天我们来学习一下ScheduledExecutorService的用法.我们都太渺小了,那么容易便湮 ...

  2. java高级---->Thread之ExecutorService的使用

    今天我们通过实例来学习一下ExecutorService的用法.我徒然学会了抗拒热闹,却还来不及透悟真正的冷清. ExecutorService的简单实例 一.ExecutorService的简单使用 ...

  3. java高级---->Thread之Phaser的使用

    Phaser提供了动态增parties计数,这点比CyclicBarrier类操作parties更加方便.它是jdk1.7新增的类,今天我们就来学习一下它的用法.尘埃落定之后,回忆别来挑拨. Phas ...

  4. java高级---->Thread之CompletionService的使用

    CompletionService的功能是以异步的方式一边生产新的任务,一边处理已完成任务的结果,这样可以将执行任务与处理任务分离开来进行处理.今天我们通过实例来学习一下CompletionServi ...

  5. java高级---->Thread之CyclicBarrier的使用

    CyclicBarrier是一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).今天我们就学习一下CyclicBarrier的用法. Cycl ...

  6. java高级---->Thread之Exchanger的使用

    Exchanger可以在两个线程之间交换数据,只能是2个线程,他不支持更多的线程之间互换数据.今天我们就通过实例来学习一下Exchanger的用法. Exchanger的简单实例 Exchanger是 ...

  7. java高级---->Thread之FutureTask的使用

    FutureTask类是Future 的一个实现,并实现了Runnable,所以可通过Excutor(线程池) 来执行,也可传递给Thread对象执行.今天我们通过实例来学习一下FutureTask的 ...

  8. java高级---->Thread之Condition的使用

    Condition 将 Object 监视器方法(wait.notify 和 notifyAll)分解成截然不同的对象,以便通过将这些对象与任意 Lock 实现组合使用,为每个对象提供多个等待 set ...

  9. java高级---->Thread之CountDownLatch的使用

    CountDownLatch是JDK 5+里面闭锁的一个实现,允许一个或者多个线程等待某个事件的发生.今天我们通过一些实例来学习一下它的用法. CountDownLatch的简单使用 CountDow ...

随机推荐

  1. 【C#/WPF】Button按钮动态设置Background背景颜色

    学习笔记: 在XAML中给Button设置颜色大家都懂的,本篇只是记录用C#代码动态生成的按钮设置Background背景颜色. new一个Button,设置Background时可看到该属性类型是S ...

  2. Hadoop计算中的Shuffle过程(转)

    Hadoop计算中的Shuffle过程 作者:左坚 来源:清华万博 时间:2013-07-02 15:04:44.0 Shuffle过程是MapReduce的核心,也被称为奇迹发生的地方.要想理解Ma ...

  3. apt-get install 的替换命令及mysql安装问题的解决

    Some packages could not be installed. This may mean that you haverequested an impossible situation o ...

  4. [LintCode]各位相加

    描述: 给出一个非负整数 num,反复的将所有位上的数字相加,直到得到一个一位的整数. 给出 num = 38. 相加的过程如下:3 + 8 = 11,1 + 1 = 2.因为 2 只剩下一个数字,所 ...

  5. Hibernate- hibernate二级缓存

    原文地址:http://www.iteye.com/topic/18904 很多人对二级缓存都不太了解,或者是有错误的认识,我一直想写一篇文章介绍一下hibernate的二级缓存的,今天终于忍不住了. ...

  6. Python if-else and while

    if-elif语法 if condition: [tab键] command elif condition: [tab键] command elif condition: [tab键] command ...

  7. 用route命令解决多出口的问题

    网络已经走进了我们的生活.工作.学习之中,大多数单位.公司都已经连接到了Internet.但是,因为各种原因,有这样一个问题存在.就是:这些单位即有到公网(Internet)的出口连接,也有到专网(单 ...

  8. JAVA虚拟机、Dalvik虚拟机和ART虚拟机简要对比

    1.什么是JVM?   JVM本质上就是一个软件,是计算机硬件的一层软件抽象,在这之上才能够运行Java程序,JAVA在编译后会生成类似于汇编语言的JVM字节码,与C语言编译后产生的汇编语言不同的是, ...

  9. LogCat大量Unexpected value from nativeGetEnabledTags: 0

    在执行模拟器的时候.LogCat 输出非常多Unexpected value from nativeGetEnabledTags: 0 提示.导致非常多本来须要输出的信息被瞬间覆盖了,查询后得知是sd ...

  10. vs2013+MVC3.0+EasyUI的ComboBox联动使用(二)

     vs2013+MVC3.0+EasyUI的ComboBox联动使用(二) 简单介绍:在vs2013(.net4.0)中使用MVC3.0对于EasyUI中ComboBox的联动使用. 载入Comb ...