java - 手写定长线程池

package com.gpdi.operatingunit.test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @description: 固定线程池大小的线程池
 * @author: Lxq
 * @date: 2020/1/10 14:33
 */
public class MyNewFixedSizeThreadPool {
    /**
     * 任务队列
     */
    private BlockingQueue queue;

    /**
     * 工作线程集合
     */
    private List<Thread> workers;

    /**
     * 线程池工作的标识
     */
    private volatile boolean working = true;


    /**
     * @param poolSize      线程池数量
     * @param tastQueueSize 任务队列的长度
     */
    public MyNewFixedSizeThreadPool(int poolSize, int tastQueueSize) {
        if (poolSize <= 0 || tastQueueSize <= 0) {
            throw new IllegalArgumentException("参数错误");
        }
        // 阻塞队列LinkedBlockingQueue
        queue = new LinkedBlockingQueue(tastQueueSize);

        // 将工作线程包装为线程安全的集合
        this.workers = Collections.synchronizedList(new ArrayList<>());
        for (int i = 0; i < poolSize; i++) {
            Worker w = new Worker(this);
            w.start();
            this.workers.add(w);
        }
    }


    /**
     * 提交线程
     *
     * @param task
     * @return
     */
    public boolean submit(Runnable task) {
        return this.queue.offer(task);
    }


    /**
     * 关闭线程池
     */
    public void shutdown() {
        this.working = false;

        // 把阻塞的线程中断
        for (Thread t : this.workers) {
            if (t.getState().equals(Thread.State.BLOCKED)
                    || t.getState().equals(Thread.State.WAITING)
                    || t.getState().equals(Thread.State.TIMED_WAITING)) {
                t.interrupt();
            }
        }
    }

    /**
     * 执行线程
     */
    private class Worker extends Thread {

        private MyNewFixedSizeThreadPool pool;
        public Worker(MyNewFixedSizeThreadPool pool) {
            super();
            this.pool = pool;
        }
        @Override
        public void run() {
            while (this.pool.working || this.pool.queue.size() > 0) {
                //执行任务
                Runnable task = null;
                try {
                    if (this.pool.working) {
                        task = (Runnable) this.pool.queue.take();
                    }else {
                        task = (Runnable) this.pool.queue.poll();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (task !=null){
                    task.run();
                    System.out.println(Thread.currentThread().getName());
                }
            }
        }
    }


}

测试:

public class Test {

    public static void main(String[] args) throws InterruptedException {

        MyNewFixedSizeThreadPool pool = new MyNewFixedSizeThreadPool(3,6);
        for (int i = 0; i < 7; i++) {
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(1);
                }
            });
        }
        pool.shutdown();

    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值