手写线程池,理清相关原理

准备工作:

  1. 需要一个任务仓库
  2. 集合容器,存放工作线程
  3. 普通线程要执行多个task,咱们需要封装一下
  4. 初始化线程池
  5. 对外提供提交任务的接口 非阻塞; 对外提供提交任务的接口 阻塞
  6. 关闭线程池
    a. 禁止往队列提交任务
    b. 等待仓库中的任务执行
    c. 关闭的时候,再去那任务就不用阻塞,因为不会有新任务来了
    d. 关闭的时候,阻塞的线程,就要强行中断阻塞/等待的线程

代码地址:
https://github.com/szwkang/tracylearn/tree/master/threadpool

代码展示:

package threadpoolimpl.fixedthreadpoolImpl.demo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
public class FixedThreadPool {

//定义pool是否shutdown
private boolean isShutDown = false;  

//1.定义任务仓库
private LinkedBlockingQueue<Thread> queue;
//2.定义工作线程组
private List<Thread> threadWorkList;

//3.封装work thread
public static class WorkThread extends Thread {
    private FixedThreadPool fixedThreadPool;
    public WorkThread(FixedThreadPool pool) {
        this.fixedThreadPool = pool;
    }

    @Override
    public void run() {
        while (!this.fixedThreadPool.isShutDown || this.fixedThreadPool.queue.size() > 0) {
            Thread task = null;
            try {
                if (!this.fixedThreadPool.isShutDown)
                    task = this.fixedThreadPool.queue.take();
                else
                    task = this.fixedThreadPool.queue.poll();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (task != null) {
                task.run();
            }
        }
    }
}

//4. init thread pool
public FixedThreadPool(int poolSize, int queueSize) {
    if (poolSize <= 0 || queueSize <= 0) {
        throw new IllegalArgumentException("illegal parameter");
    }
    this.threadWorkList = Collections.synchronizedList(new ArrayList<>(poolSize));
    this.queue = new LinkedBlockingQueue<>(queueSize);
    for (int i = 0; i < poolSize; i++) {
        WorkThread workThread = new WorkThread(this);
        workThread.start();
        this.threadWorkList.add(workThread);
    }
}

//5.1 non- block submit
public boolean nonBlockSubmit(Thread thread) {
    if (this.isShutDown = false) {
        return false;
    }
    return this.queue.offer(thread);
}

//5.2 block submit
public boolean blockSubmit(Thread thread) {
    try {
        if (this.isShutDown = false) {
            return false;
        }
        this.queue.put(thread);
        return true;
    } catch (InterruptedException e) {
        e.printStackTrace();
        return false;
    }
}
//6. 关闭线程池
 a. 禁止往队列提交任务
 b. 等待仓库中的任务执行
 c. 关闭的时候,再去那任务就不用阻塞,因为不会有新任务来了
 d. 关闭的时候,阻塞的线程,就要强行中断
public void shutdown() {
    this.isShutDown = true;
    for (Thread thread : this.threadWorkList) {
        if (thread.getState().equals(Thread.State.BLOCKED) || thread.getState().equals(Thread.State.WAITING)
                || thread.getState().equals(Thread.State.TIMED_WAITING)) {
            thread.interrupt();
        }
    }
}

}

如果还有什么不明白之处,请联系我啊

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值