线程池类采用面向接口编程的方式,这种方式有一个好处就是可以作为Spring管理的一个bean使用,不需要自己new对象,将对象的创建交给容器,岂不是美滋滋,因此基于这个考虑,首先定义一个线程池的接口ThreadPool:
package com.shuaicenglou.ThreadPool;
public interface ThreadPool {
/**
* 获取线程池中的线程数
*/
int getPoolSize();
/**
* 执行用户发布的任务
* @param job
*/
void execute(Job job);
/**
* 关闭线程池
*/
void shutdown();
public int getJobNum();
}
然后我们定义一个Job接口,这个接口就是用来给线程池执行任务用的,线程池有一个线程列表,里面是创建好的固定数目的线程,还有一个job列表,当用户向线程池提交任务之后,实际上就是将任务放到一个Job的具体实现的run方法里面去,然后将这个job丢到这个job列表中,线程池里的线程有空闲的时候就会从列表中取job来执行.
package com.shuaicenglou.ThreadPool;
/**
* 工作类,用户在向线程池中发布任务时只需实现该接口即可,若不使用线程池
* 则可直接使用该Job类开新线程
* @author shuaicenglou
*
*/
public interface Job extends Runnable {
}
然后是线程池的具体实现类
package com.shuaicenglou.ThreadPool;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class DefaultThreadPool implements ThreadPool{
//最大线程数
private static final int MAX_WORKERS_NUM = 10;
//最小线程数
private static final int MIN_WORKERS_NUM = 1;
//默认线程数
private static final int DEFAULT_WORKERS_NUM = 5;
private LinkedList<Job> jobs = new LinkedList<Job>();
private final List<Worker> workers = Collections.synchronizedList(new ArrayList<>());
public DefaultThreadPool(){
init(DEFAULT_WORKERS_NUM);
}
public DefaultThreadPool(int num){
if(num<=DEFAULT_WORKERS_NUM) init(MIN_WORKERS_NUM);
else if(num>=MAX_WORKERS_NUM) init(MAX_WORKERS_NUM);
else init(num);
}
/**
* 线程池初始化
* @param num
*/
private void init(int num){
for(int i=0;i<num;i++){
Worker w = new Worker();
workers.add(w);
Thread t = new Thread(w,"Thread-"+(i+1));
t.start();
}
}
@Override
public int getPoolSize() {
return workers.size();
}
public int getJobNum(){
synchronized (jobs) {
return jobs.size();
}
}
@Override
public void execute(Job job) {
if(job!=null){
synchronized (jobs) { //获得jobs的锁
jobs.addLast(job);
jobs.notify();
}
}
}
@Override
public void shutdown() {
for(Worker w:workers) w.shutdown();
}
class Worker implements Runnable{
private volatile boolean running = true;
public void shutdown(){
running = false;
}
@Override
public void run() {
while(running){
Job job = null;
synchronized (jobs) {
while(jobs.isEmpty()){
try {
System.out.println("任务队列中为空,等待来任务......");
jobs.wait();
} catch (InterruptedException e) {
System.out.println("线程中断");
Thread.currentThread().interrupt();
return;
}
}
job = jobs.removeLast();
}
if(job!=null) job.run();
}
}
}
}
最后是测试类:
package com.shuaicenglou.ThreadPool;
import java.util.concurrent.TimeUnit;
public class Test {
public static void main(String[] args) {
ThreadPool pool = new DefaultThreadPool();
pool.execute(new NewJob());
pool.execute(new NewJob());
pool.execute(new NewJob());
pool.execute(new NewJob());
pool.execute(new NewJob());
pool.execute(new NewJob());
pool.execute(new NewJob());
pool.execute(new NewJob());
pool.execute(new NewJob());
pool.execute(new NewJob());
pool.execute(new NewJob());
pool.execute(new NewJob());
}
}
class NewJob implements Job{
@Override
public void run() {
try {
System.out.println("睡觉中");
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}