java 中 阻塞队列BlockingQueue详解及实例

2025-05-29 0 21

java 中 阻塞队列BlockingQueue详解及实例

BlockingQueue很好的解决了多线程中数据的传输,首先BlockingQueue是一个接口,它大致有四个实现类,这是一个很特殊的队列,如果BlockQueue是空的,从BlockingQueue取东西的操作将会被阻断进入等待状态,直到BlockingQueue进了东西才会被唤醒.同样,如果BlockingQueue是满的,任何试图往里存东西的操作也会被阻断进入等待状态,直到BlockingQueue里有空间才会被唤醒继续操作。

BlockingQueue的四个实现类:

1.ArrayBlockingQueue:规定大小的BlockingQueue,其构造函数必须带一个int参数来指明其大小.其所含的对象是以FIFO(先入先出)顺序排序的.

2.LinkedBlockingQueue:大小不定的BlockingQueue,若其构造函数带一个规定大小的参数,生成的BlockingQueue有大小限制,若不带大小参数,所生成的BlockingQueue的大小由Integer.MAX_VALUE来决定.其所含的对象是以FIFO(先入先出)顺序排序的

3.PriorityBlockingQueue:类似于LinkedBlockQueue,但其所含对象的排序不是FIFO,而是依据对象的自然排序顺序或者是构造函数的Comparator决定的顺序.

4.SynchronousQueue:特殊的BlockingQueue,对其的操作必须是放和取交替完成的.

BlockingQueue的常用方法:

1)add(anObject):把anObject加到BlockingQueue里,即如果BlockingQueue可以容纳,则返回true,否则报异常

2)offer(anObject):表示如果可能的话,将anObject加到BlockingQueue里,即如果BlockingQueue可以容纳,则返回true,否则返回false.

3)put(anObject):把anObject加到BlockingQueue里,如果BlockQueue没有空间,则调用此方法的线程被阻断直到BlockingQueue里面有空间再继续.

4)poll(time):取走BlockingQueue里排在首位的对象,若不能立即取出,则可以等time参数规定的时间,取不到时返回null

5)take():取走BlockingQueue里排在首位的对象,若BlockingQueue为空,阻断进入等待状态直到Blocking有新的对象被加入为止

例子:

这个例子主要模拟了生产者和消费者之间的工作流程,是一个简单的消费者等待生产者生产产品供消费者消费的场景。

生产者:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27
package com.gefufeng;

import java.util.concurrent.BlockingQueue;

public class Producter implements Runnable{

private BlockingQueue<String> blockingQueue;

public Producter(BlockingQueue<String> blockingQueue){

this.blockingQueue = blockingQueue;

}

@Override

public void run() {

try {

blockingQueue.put("我生产的" + Thread.currentThread().getName());

System.out.println("我生产的" + Thread.currentThread().getName());

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.out.println("生产失败");

}

}

}

消费者:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26
package com.gefufeng;

import java.util.concurrent.BlockingQueue;

public class Customer implements Runnable{

private BlockingQueue<String> blockingQueue;

public Customer(BlockingQueue<String> blockingQueue){

this.blockingQueue = blockingQueue;

}

@Override

public void run() {

for(;;){

try {

String threadName = blockingQueue.take();

System.out.println("取出:" + threadName);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.out.println("取出失败");

}

}

}

}

执行类:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26
package com.gefufeng;

import java.util.concurrent.ArrayBlockingQueue;

public class Executer {

public static void main(String[] args) {

ArrayBlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<String>(2);

Producter producter = new Producter(arrayBlockingQueue);

Customer cusotmer = new Customer(arrayBlockingQueue);

new Thread(cusotmer).start();

for(;;){

try {

Thread.sleep(2000);

new Thread(producter).start();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

首先是消费者循环等待产品,当第一次循环时执行blockingQueue.take(),是拿不出任何产品的,于是进入阻塞状态,两秒后,生产者生产了一个产品,于是blockingQueue拿到产品,打印了日志,然后消费者执行第二次循环,发现blockingQueue.take()又没拿到产品,于是又进入阻塞状态。。。依次循环

感谢阅读,希望能帮助到大家,谢谢大家,对本站的支持!

原文链接:https://my.oschina.net/gef/blog/825029

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java 中 阻塞队列BlockingQueue详解及实例 https://www.kuaiidc.com/118228.html

相关文章

发表评论
暂无评论