java BlockingQueue 实现多线程资源复用

java BlockingQueue 实现多线程资源复用,第1张

java BlockingQueue 实现多线程资源复用 1、测试主类
package com.queues;

import com.google.common.collect.Lists;

import java.util.List;
import java.util.concurrent.*;

public class LineBlockingQueueTest {

    public static void main(String[] args) {
        List list = Lists.newArrayList("A1", "A2", "A3", "A4", "A5");
        //1、创建并初始化资源队列
        BlockingQueue queue = new linkedBlockingQueue(list.size());
        for (String str : list) {
            queue.add(str);
        }

        //2、设置总请求数
        int num = 20;
        //3、设置并发资源数,须大于资源队列,才能体现出等待
        ExecutorService service = Executors.newFixedThreadPool(10);
        CountDownLatch latch = new CountDownLatch(num);

        for (int i = 0; i < num; i++) {
            LineConsumer consumer = new LineConsumer("thread-" + i, queue, latch);
            service.execute(consumer);
        }

        try {
            latch.await(num * 5000, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        service.shutdown();
        System.out.println("进程结束,退出!!!");

    }
}
2、消费者线程业务类
package com.queues;

import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;


public class LineConsumer implements Runnable {
    private static final int DEFAULT_RANGE_FOR_SLEEP = 2000;

    private String name;
    private BlockingQueue queue;
    private CountDownLatch cd;

    public LineConsumer(String name, BlockingQueue queue, CountDownLatch cd) {
        this.name = name;
        this.queue = queue;
        this.cd = cd;
    }

    public void run() {
        Integer retry = 0;
        Random r = new Random();
        boolean isRunning = true;
        try {
            while (isRunning) {
                if (retry > 10) {
                    throw new Exception("系统资源紧张,请稍后再试...");
                }
                System.out.println("线程:" + name + "从队列获取资源");
                String data = queue.poll(1, TimeUnit.SECONDS);

                if (null == data) {
                    System.out.println("线程:" + name + "未获取到资源,循环等待...");
                    retry++;
                    continue;
                }

                System.out.println("线程:" + name + "获取到资源:" + data);
                Thread.sleep(r.nextInt(DEFAULT_RANGE_FOR_SLEEP));

                queue.offer(data);
                System.out.println("线程:" + name + "将资源" + data + "放回队列");
                cd.countDown();
                isRunning = false;

            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

打印输出:

线程:thread-0从队列获取资源
线程:thread-3从队列获取资源
线程:thread-0获取到资源:A1
线程:thread-1从队列获取资源
线程:thread-4从队列获取资源
线程:thread-4获取到资源:A4
线程:thread-3获取到资源:A2
线程:thread-1获取到资源:A3
线程:thread-2从队列获取资源
线程:thread-2获取到资源:A5
线程:thread-5从队列获取资源
线程:thread-7从队列获取资源
线程:thread-6从队列获取资源
线程:thread-8从队列获取资源
线程:thread-9从队列获取资源
线程:thread-3将资源A2放回队列
线程:thread-5获取到资源:A2
线程:thread-10从队列获取资源
线程:thread-5将资源A2放回队列
线程:thread-7获取到资源:A2
线程:thread-11从队列获取资源
线程:thread-7将资源A2放回队列
线程:thread-6获取到资源:A2
线程:thread-12从队列获取资源
线程:thread-4将资源A4放回队列
线程:thread-8获取到资源:A4
线程:thread-13从队列获取资源
线程:thread-0将资源A1放回队列
线程:thread-9获取到资源:A1
线程:thread-14从队列获取资源
线程:thread-10未获取到资源,循环等待...
线程:thread-10从队列获取资源
线程:thread-11未获取到资源,循环等待...
线程:thread-11从队列获取资源
线程:thread-1将资源A3放回队列
线程:thread-12获取到资源:A3
线程:thread-15从队列获取资源
线程:thread-8将资源A4放回队列
线程:thread-13获取到资源:A4
线程:thread-16从队列获取资源
线程:thread-14获取到资源:A4
线程:thread-13将资源A4放回队列
线程:thread-17从队列获取资源
线程:thread-2将资源A5放回队列
线程:thread-10获取到资源:A5
线程:thread-18从队列获取资源
线程:thread-11未获取到资源,循环等待...
线程:thread-11从队列获取资源
线程:thread-6将资源A2放回队列
线程:thread-15获取到资源:A2
线程:thread-19从队列获取资源
线程:thread-9将资源A1放回队列
线程:thread-16获取到资源:A1
线程:thread-14将资源A4放回队列
线程:thread-17获取到资源:A4
线程:thread-18未获取到资源,循环等待...
线程:thread-18从队列获取资源
线程:thread-16将资源A1放回队列
线程:thread-11获取到资源:A1
线程:thread-17将资源A4放回队列
线程:thread-19获取到资源:A4
线程:thread-10将资源A5放回队列
线程:thread-18获取到资源:A5
线程:thread-12将资源A3放回队列
线程:thread-15将资源A2放回队列
线程:thread-19将资源A4放回队列
线程:thread-11将资源A1放回队列
线程:thread-18将资源A5放回队列
进程结束,退出!!!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5708377.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存