自定义实现newFixedThreadPool

自定义实现MyFixedThreadPool

自己想深入研究一下线程池的实现,所以自定义实现一下,首先考虑,既然线程池的初始化需要一个任务队列与线程集合,具体的代码如下:

package com.bdcloud.threadpool;

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

/***
 * 自定义线程池 newFixedThreadPool
 */
public class MyFixedThreadPool {

    //存放任务
    LinkedBlockingQueue<Runnable> queue;

    //存放的工作线程
    List<Worker> workers;

    public MyFixedThreadPool(int queueSize,int workSize){

        if (queueSize<=0 || workSize<=0){
            throw new IllegalArgumentException("参数传递错误");
        }
        //初始化工作集合于工作队列
        this.queue = new LinkedBlockingQueue<>(queueSize);
        this.workers = Collections.synchronizedList(new ArrayList<>(workSize));
        //初始化所有的工作线程
        for (int i = 0; i < workSize; i++) {
            Worker worker = new Worker(this);
            worker.start();
            this.workers.add(worker);
        }
    }

    //定义工作类
    public static  class Worker extends Thread{

        private MyFixedThreadPool threadPool;

        public Worker(MyFixedThreadPool threadPool){
            this.threadPool = threadPool;
        }

        @Override
        public void run() {
            while (this.threadPool.isWorking||this.threadPool.queue.size()>0){
                Runnable take = null;
                try {
                    //从工作队列取任务并执行
                    if (this.threadPool.isWorking){
                        take = threadPool.queue.take();
                    }else {
                        take = threadPool.queue.poll();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (take!=null){
                    take.run();
                    System.out.println("当前的线程"+Thread.currentThread().getName()+"执行完毕");
                }
            }
        }
    }

    /**
     * 提交任务
     * @param runnable
     * @return
     */
    public boolean submit(Runnable runnable){
        if (this.isWorking){
            return this.queue.offer(runnable);
        }
        return false;
    }
    //判断线程池是否在运行
    private volatile  boolean isWorking = true;

    /**
     * 关闭线程池
     * 1.就是线程池停止接收任务
     * 2.若任务队列中还有数据,则继续取出并执行
     * 3.中断变量设置后,取任务的时候就不必要阻塞的取
     * 4.一旦任务阻塞,我们就应该去中断它
     */
    private void shutDown(){
        this.isWorking = false;
        for (Thread thread:workers) {
        if (thread.getState().equals(Thread.State.BLOCKED))
            thread.interrupt();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyFixedThreadPool pool = new MyFixedThreadPool(6,3);

        //向线程池提交6个任务
        for (int i = 0; i < 6; i++) {
        pool.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("提交了一个线程");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        }
        //关闭线程池
        pool.shutDown();
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章