1.简介
使用线程池可以避免线程的频繁创建以及销毁。
JAVA中提供的用于实现线程池的API:
Executor、ExecutorService、AbstractExecutorService、ThreadPoolExecutor、ForkJoinPool都位于java.util.concurrent包下。
*ThreadPoolExecutor、ForkJoinPool为线程池的实现类。
2.Executor
public interface Executor {
void execute(Runnable command);
}
*该接口声明了execute(Runnable command)方法,负责向线程池中提交一个任务。
3.ExecutorService接口
public interface ExecutorService extends Executor {
void shutdown();
List shutdownNow();
boolean isShutdown();
boolean isTerminated();
boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;
Future submit(Callable task);
Future submit(Runnable task, T result);
Future> submit(Runnable task);
List> invokeAll(Collection extends Callable> tasks) throws InterruptedException;
List> invokeAll(Collection extends Callable> tasks, long timeout, TimeUnit unit) throws InterruptedException;
T invokeAny(Collection extends Callable> tasks) throws InterruptedException, ExecutionException;
T invokeAny(Collection extends Callable> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;
}
public interface Future{ boolean cancel(boolean mayInterruptIfRunning); boolean isCancelled(); boolean isDone(); V get() throws InterruptedException, ExecutionException; V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; }
*execute()方法不能获取任务的执行结果,而submit()方法能够根据返回的Future实例获取任务的执行结果。
4.ThreadPoolExecutor
corePoolSize:线程池中核心线程的数量。
maximumPoolSize:线程池中最大线程数。
keepAliveTime:线程的空闲时间。
unit:修饰线程空闲时间的单位。
workQueue:任务队列。
threadFactory:线程工厂,用于创建线程。
handler:当队列已满且当前线程数已达到所允许的最大值时的处理策略。
*线程池中的线程包括核心线程以及普通线程,核心线程一旦创建后直到线程池被关闭前都就不会被销毁,而普通线程会因为到达空闲时间而被销毁。
构造方法:
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
BlockingQueue的类型
BlockingQueue提供了ArrayBlockingQueue、linkedBlockingQueue、SynchronousQueue等实现类。
1.ArrayBlockingQueue:使用顺序表的结构进行存储,在使用时需要指定其长度,支持公平锁/非公平锁进行操作。
2.linkedBlockingQueue:使用链表的结构进行存储,在使用时不需要指定其长度,队列的最大长度为Integer.MAX_VALUE。
3.SynchronousQueue:一个不存储元素的队列,每一个put操作必须等待take操作,否则不能添加元素,支持公平锁和非公平锁。
*这些实现类在进行入队和出队操作时都会进行加锁,以保证在多线程并发访问时数据的安全性。
队列已满且线程数已达到所允许的最大值时的处理策略
RejectedExecutionHandler提供了AbortPolicy、DiscardPolicy、DiscardOlderstPolicy、CallerRunsPolicy四个策略,这四个策略都是ThreadPoolExecutor的静态内部类。
1.AbortPolicy:放弃任务并抛出RejectedExecutionException异常。
2.DiscardPolicy:放弃任务但不抛出异常。
3.DiscardOlderstPolicy: 放弃队头中的任务,然后重新尝试执行新任务。
4.CallerRunsPolicy: 由调用线程来处理该任务。
线程池的状态
private static final int RUNNING = -1; private static final int SHUTDOWN = 0; private static final int STOP = 1; private static final int TIDYING = 2; private static final int TERMINATED = 3;
1.RUNING:线程池处于运行状态,此时可以接受新的任务请求,并且执行队列中的任务。
2.SHUTDOWN:线程池处于关闭状态,此时不接受新的任务请求,但会继续执行队列中的任务。
3.STOP:线程池处于禁用状态,此时不接受新的任务请求,并且不会执行队列中的任务。
4.TIDYING:线程池处于整理状态,此时没有正在执行的任务。
5.TERMINATED :线程池处于终止状态。
线程池状态的变化过程
1.当线程池创建后处于RUNNING状态。
2.1 若此时调用了shutdown()方法,那么线程池将处于SHUTDOWN状态,不接受新的任务请求,但会继续执行队列中的任务,当队列中的任务为空且没有正在执行的任务时,线程池的状态为TIDYING。
2.2 若此时调用了shutdownNow()方法,那么线程池将处于STOP状态,不接受新的任务请求并且不执行队列中的任务,此时线程池的状态为TIDYING。
3.当线程池的状态为TIDYING时,当terminated()方法处理完毕后,线程池的状态为TRRMINATED。
任务的执行流程
1.当调用了execute()或者submit()方法向线程池提交一个任务后,首先判断当前线程池中的线程个数是否大于核心线程数。
2.如果当前线程池的线程个数小于核心线程数,则创建一个核心线程来处理任务。
3.如果当前线程池的线程个数大于核心线程数,则将任务放入到队列中,如果放入队列成功,那么该任务将等待被空闲的线程处理,如果放入队列失败(队满),则判断当前线程池中的线程个数是否达到所允许的最大值,若未达到则创建一个普通线程去处理任务,否则根据预定义的处理策略去进行处理。
5.Executors工具类
JAVA中提供了Executors工具类,用于直接创建Executor。
CacheThreadPool
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,60L, TimeUnit.SECONDS,new SynchronousQueue());
}
CacheThreadPool创建的都是普通线程(其核心线程数为0)、线程池的最大线程数为Integer.MAX_VALUE、线程的空闲时间为60秒,此方式适合大量耗时短的任务、不适合大量耗时长的任务。
*由于创建的都是普通线程,且空闲时间为60秒,则仍有可能会频繁的创建线程。
FixedThreadPool
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,new linkedBlockingQueue());
}
FixedThreadPool创建的都是核心线程,其线程个数由入参决定,线程不会因为空闲时间而被销毁,适合预知任务数量的业务。
SingleThreadExecutor
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService(new ThreadPoolExecutor(1, 1,new linkedBlockingQueue()));
}
SingleThreadExecutor使用一个核心线程来处理任务。
ScheduledThreadPool
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
*ScheduledThreadPool支持定时执行任务以及固定间隔执行任务。
SingleThreadScheduledExecutor
public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
return new DelegatedScheduledExecutorService(new ScheduledThreadPoolExecutor(1));
}
*SingleThreadScheduledExecutor支持一个线程的定时执行任务以及固定间隔执行任务。
public interface ScheduledExecutorService extends ExecutorService {
public ScheduledFuture> schedule(Runnable command,long delay, TimeUnit unit);
public ScheduledFuture schedule(Callable callable,long delay, TimeUnit unit);
public ScheduledFuture> scheduleAtFixedRate(Runnable command,long initialDelay,long period,TimeUnit unit);
public ScheduledFuture> scheduleWithFixedDelay(Runnable command,long initialDelay,long delay,TimeUnit unit);
}
WorkStealingPool
public static ExecutorService newWorkStealingPool(int parallelism) {
return new ForkJoinPool(parallelism,ForkJoinPool.defaultForkJoinWorkerThreadFactory,null, true);
}
WorkStealingPool创建一个并行级别的线程池,同一时刻最多只能有指定个数个线程正在执行任务,创建时直接指定同一时刻最多能允许的并行执行的线程个数即可,如果不传则使用CPU的核数。
newWorkStealingPool方法内部返回一个ForkJoinPool实例,ForkJoinPool是JAVA7新提供的线程池,同样继承AbstactExecutorService。
*作用类似于Semaphore。
以上所述是小编给大家介绍的Java中的线程池详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!



