手写线程池 (一)

前言准备

1.jdk线程池的使用:https://www.cnblogs.com/jtfr/p/10187419.html

2.线程池核心:线程的复用。

  运行的线程是线程池的核心,被添加的任务需要实现过Runnable接口,主要是保证有run方法。运行时候 对象.run() 。

一、手写线程池注意要点

1.线程池需要添加任务,任务是放置在一个队列(FIFO)当中,具体只要保证FIFO,或优先级保证(Map集合)先执行。
2.线程池运行,需要一个容器存放创建的线程,可数组或集合,可以自己设计思考。
3.编写:先抽象写接口,后编码写实现类

二、线程池实现

1.编写添加任务和销毁任务的接口

 1 package com.jtfr.MyThreadPool;
 2 
 3 import java.util.List;
 4 
 5 /**
 6  * 线程池接口
 7  * @author 陈康明 qq:1123181523
 8  * @date 2019年2月3日
 9  * ENCODEING = "UTF-8"
10  */
11 public interface IThreadPool {
12 
13     /**
14      * 添加单个任务
15      */
16     void executor(Runnable task);
17     
18     /**
19      * 添加多个任务
20      */
21     void executor(List<Runnable> task);
22     
23     /**
24      * 销毁工作线程,保留核心线程。
25      */
26     void destory(int core);
27     
28     /**
29      * 销毁线程池
30      */
31     void destory();
32 }

2.

猜你喜欢

转载自www.cnblogs.com/jtfr/p/10350238.html