手写一个简单的线程池

import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 *类说明:简陋的自定义线程池实现
 */
public class MyThreadPool {

    /*缺省线程池大小*/
    private static int POOL_SIZE = 5;
    /*缺省阻塞队列大小*/
    private static int QUEUE_SIZE = 100;

    /*存放任务*/
    private final BlockingQueue<Runnable> taskQueue;
    /*工作线程*/
    private WorkThread[] workThreads;
    private final int pool_size;

    public MyThreadPool(){
        this(QUEUE_SIZE,POOL_SIZE);
    }
    public MyThreadPool(int pool_size){
        this(QUEUE_SIZE,pool_size);
    }
 
    public MyThreadPool(int queue_size,
                         int pool_size) {
        if (pool_size<=0) pool_size = POOL_SIZE;
        if (queue_size<=0) queue_size = QUEUE_SIZE;
        this.taskQueue = new ArrayBlockingQueue<>(queue_size);
        this.pool_size = pool_size;
        workThreads = new WorkThread[pool_size];
        /*工作线程准备好了*/
        for(int i=0;i<pool_size;i++){
            workThreads[i] = new WorkThread();
            workThreads[i].start();
        }
    }

    /*内部类,工作线程的实现*/
    private class WorkThread extends Thread{
        @Override
        public void run() {
            Runnable r ;
            try {
                while(!isInterrupted()){
                    r = taskQueue.take();
                    if(r!=null){
                        System.out.println(Thread.currentThread().getId()+" ready execute"
                                +((MyTask)r).getName());
                        r.run();
                    }
                   r = null;
                }
            } catch (InterruptedException e) {
              //take()不到了就会抛出异常
             //e.printStackTrace();
            }

        }

        /*停止工作*/
        public void stopWorker() {
            interrupt();
        }
    }


    /*销毁线程池*/
    public void destroy(){
        System.out.println("ready close pool....");
        for(int i=0;i<pool_size;i++){
            workThreads[i].stopWorker();
            workThreads[i] = null;//help gc
        }
        taskQueue.clear();
    }

    /*放入任务,但是只是加入队列*/
    public void execute(Runnable task){
        try {
            taskQueue.put(task);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    @Override
    public String toString() {
        return "WorkThread number:"+pool_size
                +" wait task number:"+taskQueue.size();
    }
    // 任务类
    static class MyTask implements Runnable {

        private String name;
        private Random r = new Random();

        public MyTask(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        @Override
        public void run() {
        	// 执行任务
            try {
                Thread.sleep(r.nextInt(1000)+2000);
            } catch (InterruptedException e) {
                System.out.println(Thread.currentThread().getId()+" sleep InterruptedException:"
                        +Thread.currentThread().isInterrupted());
            }
            System.out.println("任务 " + name + " 完成");
        }
    }
    public static void main(String[] args) throws InterruptedException {

        MyThreadPool t = new MyThreadPool(3);
        t.execute(new MyTask("testA"));
        t.execute(new MyTask("testB"));
        t.execute(new MyTask("testC"));
        t.execute(new MyTask("testD"));
        t.execute(new MyTask("testE"));
        System.out.println(t);
        Thread.sleep(10000);
        t.destroy();// 所有线程都执行完成才destory
        System.out.println(t);
    }


}

运行结果:
在这里插入图片描述

参考:Mark—笔记_Java并发编程

©️2020 CSDN 皮肤主题: 岁月 设计师: pinMode 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值