1、首先由几个接口和类的关系是需要先说明的:
extends implements extends
Executor(接口)----------------->ExecutorService(接口)------------------->AbstractExecutorService(抽象类)------------->ThreadpoolExecutor
(execute()) (submit())
2、对ThreadPoolExecutor(是ExecutorService的默认实现类)的理解:
他有四个构造函数:
public class ThreadPoolExecutor extends AbstractExecutorService {
.....
public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,
BlockingQueue workQueue);
public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,
BlockingQueue workQueue,ThreadFactory threadFactory);
public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,
BlockingQueue workQueue,RejectedExecutionHandler handler);
public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,
BlockingQueue workQueue,ThreadFactory threadFactory,RejectedExecutionHandler handler);
...
}
其中最为第四个比较完善,我们就拿这个构造函数里面的参数进行说明:
corePoolSize:是线程池的大小,刚开始创建时里面是没有线程的,而是等待有任务时才开始创建线程,也可以用prestartAllCoreThreads()或者prestartCoreThread()方法进行欲创建线程,即在没有任务到来之前就创建corePoolSize个线程或者一个线程。默认情况下,在创建了线程池后,线程池中的线程数为0,当有任务来之后,就会创建一个线程去执行任务,当线程池中的线程数目达到corePoolSize后,就会把到达的任务放到缓存队列当中;
maxnumPoolSize:是线程池最大可扩充大小
keepAliveTime:表示线程没有任务执行时经过多长时间终止,基本上是当线程池中的线程达到corePoolSize后才会考虑到。
unit:是keepAliveTime的时间单位:可以有day,hour,minutes,seconds,ms,ns等
workQueue:是阻塞队列,有ArrayBlockingQueue和PriorityBlockingQueue,一般使用LinkedBlockingQueue和Synchronous。线程池的排队策略与BlockingQueue有关。
threadFactory:线程工厂,主要用来创建线程;
handler:表示当拒绝处理任务时的策略。
下面是用构造函数进行的编程:
package threadPoll;
/**
* Created by Administrator on 2015/11/2 0002.
*/
public class MyTask implements Runnable{
int num;
public MyTask(int num) {
this.num=num;
}
public void run() {
System.out.println("Is doing..task"+num);
try{
Thread.currentThread().sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("task"+num+" is done.");
}
}
package threadPoll;
import java.util.concurrent.*;
/**
* Created by Administrator on 2015/11/2 0002.
*/
public class MyTest {
public static void main(String[] args) {
ThreadPoolExecutor pool=new ThreadPoolExecutor(5,8,200, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(5));
for(int i=0;i<15;i++){
MyTask myTask=new MyTask(i);
pool.execute(myTask);
System.out.println("now threadcorepoolsize is "+pool.getPoolSize()
+ ", cachepoolsize is" +pool.getQueue().size()
+",is completedsize is"+pool.getCompletedTaskCount());
}
pool.shutdown();
}
}
下面常用的三个方法可以用来代替构造函数,因为本身他们就是用构造函数定义的:
三个静态方法的实现,是根据ThreadPoolExecutor的构造函数,只不过所给的缓冲池的大小,最大容量大小,阻塞队列存储方式不同:
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue());
}
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue()));
}
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue());
}
定义MyThread类:
package threadPool;
/**
* Created by Administrator on 2015/11/2 0002.
*/
public class MyThread extends Thread {
public void run(){
System.out.println(Thread.currentThread().getName()+" is doing...");
try{
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package threadPool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Created by Administrator on 2015/11/2 0002.
*/
public class ThreadPool {
public static void main(String[] args) {
ExecutorService pool= Executors.newFixedThreadPool(2);
//ExecutorService pool=Executors.newSingleThreadExecutor();
Thread t1=new MyThread();
Thread t2=new MyThread();
Thread t3=new MyThread();
Thread t4=new MyThread();
pool.execute(t1);//将任务提交到线程池,但是newSingleThreadExecutor只开辟一个线程的大小,
// newFixThreadPool()是开辟一定空间大小的线程池。
pool.execute(t2);
pool.execute(t3);
pool.execute(t4);
pool.shutdown();
}
}