线程池
Posted aaroncnblogs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线程池相关的知识,希望对你有一定的参考价值。
线程池继承链
public class ThreadPoolExecutor extends AbstractExecutorService
public abstract class AbstractExecutorService implements ExecutorService
public interface ExecutorService extends Executor
void shutdown();
Future<?> submit(Runnable task);
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
public interface Executor
void execute(Runnable command);
构造方法
ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler)
队列
private BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(int capacity);
队列继承链
public class ArrayBlockingQueue<E> extends AbstractQueue<E>
implements BlockingQueue<E>, java.io.Serializable
handler:
ThreadPoolExecutor.AbortPolicy()
抛出java.util.concurrent.RejectedExecutionException异常
ThreadPoolExecutor.CallerRunsPolicy()
重试添加当前的任务,他会自动重复调用execute()方法
ThreadPoolExecutor.DiscardOldestPolicy()
抛弃旧的任务
ThreadPoolExecutor.DiscardPolicy()
抛弃当前的任务
周期线程池:
继承链
public class ScheduledThreadPoolExecutor
extends ThreadPoolExecutor
implements ScheduledExecutorService
public interface ScheduledExecutorService extends ExecutorService
延时执行
public ScheduledFuture<?> schedule(Runnable command,
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);
构造方法
ScheduledThreadPoolExecutor(int corePoolSize)
防止任务执行遇到异常取消后续执行
public void run() {
//捕获所有的异常,保证定时任务能够继续执行
try {
//doBusiness();
} catch (Throwable e) {
// donothing
}
}
以上是关于线程池的主要内容,如果未能解决你的问题,请参考以下文章