为了更好的控制多线程,JDK提供了一套线程框架Executor,帮助开发人员有效进行线程控制。他们都在java.util.concurrent包中,是JDK并发包的核心。其中有一个比较重要的类Executors,扮演了线程工厂的角色,通过它可以创建特定功能的线程池。
Executors创建线程池的方法:
/** * 以下四种底层实现核心都是实例化了一个ThreadPoolExecutor对象返回,其实就是根据以下四种传递的具体参数 * 不同而实现的四种不同的功能。 * * ThreadPoolExecutor来实现以下四种的原因是,当以下四种不满足我们需求时,我们可以 * 自定义线程池。 * 构造参数: * public ThreadPoolExecutor( * int corePoolSize,--当前线程池核心线程数 * int maximumPoolSize,--当前线程池最大线程数 * long keepAliveTime,--保持活着的空间时间 * TimeUnit unit,--时间单位 * BlockingQueue<Runnable> workQueue,--排队等待的自定义队列 * ThreadFactoty threadFactory, * RejectedExecutionHandler handler--队列满以后,其他任务被拒绝执行的方法 * ){.........} * */
newFiexedThreadPool
该方法返回一个固定数量的线程池,该方法的线程数量始终不变,当有一个任务提交时,若线程池中空闲,则立即执行,若没有则会被暂缓在一个任务队列中等待有空闲的线程去执行。
/* * public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); } * 核心线程数=最大线程数,空闲时间0s,线程执行完直接回收,但是维持线程池个数固定, * 没有空闲线程的话,线程会被缓存在LinkedBlockingQueue无界阻塞队列中 */ // ExecutorService pool = Executors.newFixedThreadPool(10);
newSingleThreadExecutor
创建有一个线程的线程池,若空闲则执行,若没有空闲则暂缓在任务队列中。
/* * 和固定差不多,仅有1个线程 * public static ExecutorService newSingleThreadExecutor() { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>())); } */ // ExecutorService pool = Executors.newSingleThreadExecutor();
newCachedThreadPool
返回一个可以根据实际情况调整线程个数的线程池,不限制最大线程数量,若有空闲的线程则执行任务,若无任务则不创建线程,并且空闲线程会在60s后自动回收。
/** * 初始化时核心线程0,不限制线程最大数, * 只要任务来了就创建一个线程并且执行任务,使用SynchronousQueue,没有任何容量。 * 线程最大空闲时间60s,如果线程在60s内没有任务到来就回收线程。如果59s的时候来了,就去执行 * 任务不会回收线程。 * * public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); } */ // ExecutorService pool = Executors.newCachedThreadPool();
newScheduledThreadPool
该方法返回一个ScheduledExecutorService对象,但该线程池可以指定线程的数量。
/** * 初始化指定的核心线程数,且没有最大线程数限制,空闲时间0s,执行完任务就回收线程, * 线程没有空闲时任务到来,放到DelayedWorkQueue中。只有等延迟时间到了,任务才能被取出执行。 * * * public ScheduledThreadPoolExecutor(int corePoolSize) { super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS, new DelayedWorkQueue()); } */ // ExecutorService pool = Executors.newScheduledThreadPool(10);
举例:
import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; class Temp extends Thread { public void run() { System.out.println("run"); } } public class ScheduledJob { public static void main(String args[]) throws Exception { Temp command = new Temp(); ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); /** * scheduleWithFixedDelay( * Runnable command, 具体线程任务 * long initialDelay, 初始化延迟执行时间 * long delay, 轮询执行时间,每隔delay时间执行一次 * TimeUnit unit 单位 * ) * */ //5s后,每隔1s执行一次command ScheduledFuture<?> scheduleTask = scheduler.scheduleWithFixedDelay(command, 5, 1, TimeUnit.SECONDS); } }
打印:
run run run run