阻塞队列 java实现_Java实现阻塞队列的两种方式

方式一:

import java.util.PriorityQueue;

/**

* 使用非阻塞队列PriorityQueue以及notify(),wait()机制实现一个阻塞队列

* @author user

*

*/

public class MyBlockingQueue {

public final static int queueSize = 10;

public static PriorityQueue queue = new PriorityQueue<>();

public static void main(String[] args) {

Product p = new Product();

Consumer c = new Consumer();

Thread t1 = new Thread(p);

Thread t2 = new Thread(c);

t1.start();

t2.start();

}

}

//生产者

class Product implements Runnable{

@Override

public void run() {

product();

}

public void product() {

while(true) {

synchronized(MyBlockingQueue.queue) {

while(MyBlockingQueue.queue.size() == MyBlockingQueue.queueSize){

try {

System.out.println("仓库已经放不下烤猪了,赶快来吃吧。烤猪数量:"+MyBlockingQueue.queueSize);

MyBlockingQueue.queue.wait();

} catch (InterruptedException e) {

e.printStackTrace();

notify();

}

}

MyBlockingQueue.queue.offer(1);

System.out.println("我烤了一头猪。烤猪数量:" + MyBlockingQueue.queue.size());

MyBlockingQueue.queue.notify();

System.out.println();

}

}

}

}

//消费者

class Consumer implements Runnable{

@Override

public void run() {

consumer();

}

private void consumer() {

while(true) {

synchronized(MyBlockingQueue.queue){

while(MyBlockingQueue.queue.size() == 0) {

try{

System.out.println("没有烤猪了,赶快生产一个。烤猪数量:"+MyBlockingQueue.queue.size());

MyBlockingQueue.queue.wait();

} catch (InterruptedException e) {

e.printStackTrace();

notify();

}

}

MyBlockingQueue.queue.poll();

System.out.println("吃掉了一头烤猪。烤猪数量:" + MyBlockingQueue.queue.size());

MyBlockingQueue.queue.notify();

}

}

}

}

方式二:

import java.util.concurrent.ArrayBlockingQueue;

/**

* 使用阻塞队列ArrayBlockingQueue实现生产者消费者问题

* @author user

*

*/

public class MyBlockingQueue2 {

public final static int queueSize = 10;

public static ArrayBlockingQueue queue = new ArrayBlockingQueue<>(queueSize);

public static void main(String[] args) {

Product2 p = new Product2();

Consumer2 c = new Consumer2();

Thread t1 = new Thread(p);

Thread t2 = new Thread(c);

t1.start();

t2.start();

}

}

//生产者

class Product2 implements Runnable{

@Override

public void run() {

product();

}

public void product() {

while(true) {

try{

MyBlockingQueue2.queue.put(1);

System.out.println("生产了一头烤猪。烤猪数量:" + MyBlockingQueue2.queue.size());

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

//消费者

class Consumer2 implements Runnable{

@Override

public void run() {

consumer();

}

private void consumer() {

while(true) {

try{

MyBlockingQueue2.queue.take();

System.out.println("吃掉了一头烤猪。烤猪数量:" + MyBlockingQueue2.queue.size());

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用阻塞队列实现的示例代码: ```java import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; public class BlockingQueueExample { public static void main(String[] args) throws InterruptedException { // 创建一个大小为10的阻塞队列 BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(10); // 生产者线程 Thread producerThread = new Thread(() -> { try { for (int i = 1; i <= 20; i++) { // 将数据放入队列中 queue.put(i); System.out.println("生产者放入数据:" + i); Thread.sleep(500); // 休眠500毫秒 } } catch (InterruptedException e) { e.printStackTrace(); } }); // 消费者线程 Thread consumerThread = new Thread(() -> { try { for (int i = 1; i <= 20; i++) { // 从队列中取出数据 int data = queue.take(); System.out.println("消费者取出数据:" + data); Thread.sleep(1000); // 休眠1秒 } } catch (InterruptedException e) { e.printStackTrace(); } }); // 启动线程 producerThread.start(); consumerThread.start(); // 等待线程执行完毕 producerThread.join(); consumerThread.join(); } } ``` 在上面的示例中,我们使用了 `ArrayBlockingQueue` 来创建一个大小为10的阻塞队列。生产者线程不断往队列中放入数据,直到放入了20个数据为止;消费者线程不断从队列中取出数据,直到取出了20个数据为止。由于队列阻塞的,当队列为空时,消费者线程将被阻塞,直到有数据可取;当队列已满时,生产者线程将被阻塞,直到有空间可用。通过这种方式,我们可以很方便地实现线程间的数据传输。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值