【Thread】手写实现简易BlockingQueue及线程池ThreadPool

本文展示了如何使用ReentrantLock和Condition实现一个简单的BlockingQueue,以及基于此实现一个线程池MyThreadPool。线程池通过工作线程MyWorker实现任务执行,确保在核心线程数内创建新线程,或者将任务放入阻塞队列等待执行。此外,还提供了测试代码来验证其功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、BlockingQueue代码

  • 使用ReentrantLock实现上锁;
  • 使用Condition实现阻塞式添加与弹出元素;
public class MyBlockingQueue<T> {
    private int capacity;
    private ArrayDeque<T> queue;
    private ReentrantLock lock;
    private Condition full;
    private Condition empty;


    public MyBlockingQueue(int capacity, boolean fair) {
        this.capacity = capacity;
        queue = new ArrayDeque<>();
        lock = new ReentrantLock();
        full = lock.newCondition();
        empty = lock.newCondition();
    }

    /**
     * 阻塞放入
     */
    public void put(T t) {
        if (t == null) {
            throw new NullPointerException();
        }
        lock.lock();
        try {
            while (queue.size() == capacity) {
                full.await();
            }
            queue.add(t);
            empty.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    /**
     * 阻塞式获取
     */
    public T poll() {
        T t = null;
        lock.lock();
        try {
            while (queue.size() == 0){
                empty.await();
            }
            t = queue.poll();
            full.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
            return t;
        }
    }

}

二、TheadPool

  • worker内部类,实现Runable接口,使用while循环实现线程复用;
  • 提供execute方法:
    1.当线程池中线程数小于coreSize,创建新worker进程执行任务;
    2.当线程池中线程数不小于coreSize,将任务放入阻塞队列等待空闲线程;
  • 提供shutdown方法,销毁线程池;
public class MyThreadPool {
    private int coreSize;
    private volatile boolean running;
    private Set<MyWorker> workers;
    private MyBlockingQueue<Runnable> queue;

    public MyThreadPool(int coreSize, MyBlockingQueue<Runnable> queue){
        this.coreSize = coreSize;
        this.running = true;
        this.workers = new HashSet<>();
        this.queue = queue;
    }
    public void execute(Runnable task){
        if (task == null){
            throw new NullPointerException();
        }
        if (workers.size() < coreSize){
            MyWorker myWorker = new MyWorker(task);
            workers.add(myWorker);
            myWorker.thread.start();
        }else {
            queue.put(task);
        }
    }
    public void shutdown(){
        this.running = false;
    }


    class MyWorker implements Runnable{
        private Runnable task;
        private Thread thread;

        public MyWorker(Runnable task){
            this.task = task;
            this.thread = new Thread(this);// !!!
        }

        @Override
        public void run(){
                while (running && (task != null || (task = queue.poll()) != null)){
                    task.run();
                    task = null;// !!!
                }
        }
    }

}

三、测试

public class Test {
    public static void main(String[] args) {
        MyBlockingQueue<Runnable> queue = new MyBlockingQueue<>(5, false);
        MyThreadPool pool = new MyThreadPool(2, queue);
        for (int i =0; i < 20; i++){
            final int j = i;
            pool.execute(() -> System.out.println(j + "____" + Thread.currentThread().getName()));
        }
//        pool.shutdown();

    }
}

运行结果:

0____Thread-0
1____Thread-1
2____Thread-0
3____Thread-1
4____Thread-0
5____Thread-1
6____Thread-0
7____Thread-1
9____Thread-1
8____Thread-0
10____Thread-1
12____Thread-1
11____Thread-0
13____Thread-1
14____Thread-0
15____Thread-1
16____Thread-0
17____Thread-1
18____Thread-0
19____Thread-1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值