阻塞队列使用---ArrayBlockingQueue

ArrayBlockingQueue是JAVA5中的一个阻塞队列,能够自定义队列大小,当插入时,如果队列已经没有空闲位置,那么新的插入线程将阻塞到该队列,一旦该队列有空闲位置,那么阻塞的线程将执行插入。从队列中取数据为:take,放数据为:put。下面的例子模拟了两个队列的插入和获取,首先在队列2中插入一个数据,启动线程2向队列2中插入数据时,该线程将阻塞在队列2等待,同时启动线程1向队列1中插入数据,由于队列1此时为空那么能够正确插入,然后从队列2中取数据,当线程1再次插入时,阻塞到队列1,此时,阻塞在队列2的线程2能够插入,并且从队列1中取数据,此时线程1能够插入,如此往复50次,并且每次插入成功后都循环输出10次。代码如下:

import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; public class ArrayBlockingCommunication { public static void main(String[] args) { final Business business = new Business(); new Thread(new Runnable(){ public void run() { for(int i=0; i<50; i++){ business.sub(i); } } }).start(); new Thread(new Runnable(){ public void run() { for(int i=0; i<50; i++){ business.main(i); } } }).start(); } static class Business{ BlockingQueue<Integer> queue1 = new ArrayBlockingQueue<Integer>(1); BlockingQueue<Integer> queue2 = new ArrayBlockingQueue<Integer>(1); { try{ queue2.put(1); }catch(Exception e){ e.printStackTrace(); } } public void sub(int i){ try { queue1.put(1); System.out.println("线程" + Thread.currentThread().getName() + "正在阻塞"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("线程" + Thread.currentThread().getName() + "开始运行"); for(int j=1; j<=10; j++){ System.out.println("sub thread sequence is " + j + " loop of " + i); } try { queue2.take(); } catch (InterruptedException e) { e.printStackTrace(); } } public void main(int i){ try { queue2.put(1); System.out.println("线程" + Thread.currentThread().getName() + "正在阻塞"); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("线程" + Thread.currentThread().getName() + "开始运行"); for(int j=1; j<=10; j++){ System.out.println("main thread sequence is " + j + " loop of " + i); } try { queue1.take(); } catch (InterruptedException e) { e.printStackTrace(); } } } }


请大家注意:在Business类中有一个匿名构造函数,其特点如下:

匿名构造方法,在任何构造方法之前被调用。这样保证我们初始化Business类时已经向队列2中插入了数据,这样执行起来保证我们看到的线程1先运行,然后是线程2。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中使用阻塞队列需要进行以下步骤: 1. 导入java.util.concurrent.BlockingQueue包。 2. 创建一个阻塞队列对象,使用ArrayBlockingQueue、LinkedBlockingQueue或PriorityBlockingQueue等实现类。 3. 在Spring Boot中定义一个Bean,用于开启一个线程来消费阻塞队列中的元素。 4. 在Bean中使用@PostConstruct注解,启动线程,并在线程使用阻塞队列的take()方法来获取元素。 5. 在需要将元素加入阻塞队列的地方,使用阻塞队列的put()方法来添加元素。 以下是一个简单的示例代码: ```java import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import javax.annotation.PostConstruct; import org.springframework.stereotype.Component; @Component public class BlockingQueueExample { private BlockingQueue<String> queue = new ArrayBlockingQueue<String>(10); @PostConstruct public void init() { Thread thread = new Thread(() -> { while (true) { try { String element = queue.take(); System.out.println("消费者消费了元素:" + element); } catch (InterruptedException e) { e.printStackTrace(); } } }); thread.start(); } public void produce(String element) throws InterruptedException { System.out.println("生产者生产了元素:" + element); queue.put(element); } } ``` 在上述代码中,BlockingQueueExample类中的init()方法使用@PostConstruct注解来启动一个线程,这个线程会一直循环,使用阻塞队列的take()方法来获取元素并进行消费。produce()方法用于将元素加入阻塞队列中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值