线程池:Executor 管理多个异步任务的执行,而无需程序员显式地管理线程的生命周期。这里的异步是指多个任务的执行互不干扰,不需要进行同步操作。
主要有四种线程池:
缓存池
newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
下面是它的使用方法:
package com.xzj; import java.util.Date; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; // @ author :zjxu time:2019/1/5 public class Main { public static void main(String[] args) { ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); for (int i = 0; i < 10; i++) { cachedThreadPool.execute(new MyRunnable()); } cachedThreadPool.shutdown(); } } class MyRunnable implements Runnable { @Override public void run() { System.out.println(new Date().toString()); try { Thread.sleep(2000); } catch (InterruptedException ie) { ie.printStackTrace(); } } }
newFixedThreadPool可以创建一个定长的线程池。定长线程池最多只能同时执行一定个数的线程,这个容量在new的时候设定。
下面是一个示例:
package com.xzj; import java.util.Date; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; // @ author :zjxu time:2019/1/5 public class Main { public static void main(String[] args) { ExecutorService cachedThreadPool = Executors.newFixedThreadPool(3); for (int i = 0; i < 10; i++) { cachedThreadPool.execute(new MyRunnable()); } cachedThreadPool.shutdown(); } } class MyRunnable implements Runnable { @Override public void run() { System.out.println(new Date().toString()); try { Thread.sleep(2000); } catch (InterruptedException ie) { ie.printStackTrace(); } } }
运行结果:
Sat Jan 05 11:59:13 CST 2019 Sat Jan 05 11:59:13 CST 2019 Sat Jan 05 11:59:13 CST 2019 Sat Jan 05 11:59:15 CST 2019 Sat Jan 05 11:59:15 CST 2019 Sat Jan 05 11:59:15 CST 2019 Sat Jan 05 11:59:17 CST 2019 Sat Jan 05 11:59:17 CST 2019 Sat Jan 05 11:59:17 CST 2019 Sat Jan 05 11:59:19 CST 2019
定时池
newScheduledThreadPool创建一个定长线程池,支持延迟执行和周期性任务执行。后一种执行方式类似于单片机的定时器中断。
1、延迟执行
package com.xzj; import java.util.Date; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; // @ author :zjxu time:2019/1/5 public class Main { public static void main(String[] args) { ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5); System.out.println(new Date().toString()); scheduledThreadPool.schedule(new MyRunnable(), 3, TimeUnit.SECONDS); } } class MyRunnable implements Runnable { @Override public void run() { System.out.println(new Date().toString()); } }
结果:
SatJan0512:15:35CST2019
SatJan0512:15:38CST2019
1、周期执行
scheduleAtFixedRate(x,x,x,x)的第二个参数的意义是初始化延迟,第三个参数的意义是定时间隔延迟。
package com.xzj; import java.util.Date; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; // @ author :zjxu time:2019/1/5 public class Main { public static void main(String[] args) { ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5); System.out.println(new Date().toString()); scheduledThreadPool.scheduleAtFixedRate(new MyRunnable(), 1, 3, TimeUnit.SECONDS); try { Thread.sleep(15 * 1000); } catch (InterruptedException ie) { ie.printStackTrace(); } scheduledThreadPool.shutdown(); } } class MyRunnable implements Runnable { @Override public void run() { System.out.println(new Date().toString()); } }
运行的结果
1 Sat Jan 05 12:21:07 CST 2019 2 Sat Jan 05 12:21:08 CST 2019 3 Sat Jan 05 12:21:11 CST 2019 4 Sat Jan 05 12:21:14 CST 2019 5 Sat Jan 05 12:21:17 CST 2019 6 Sat Jan 05 12:21:20 CST 2019
结果中第一二行的原因就是初始化延迟参数形成的。
newSingleThreadExecutor创建一个单线程化的线程池,这个线程池当前池中的线程死后(或发生异常时),才能重新启动新的一个线程来替代原来的线程继续执行下去。也就是说按照单线程的模式,会按照线程添加的顺序,一个一个的执行这些线程的工作。
package com.xzj; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; // @ author :zjxu time:2019/1/5 public class Main { public static void main(String[] args) { ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); for (int i = 0; i < 10; i++) { final int index = i; singleThreadExecutor.execute(new MyRunnable(index)); } } } class MyRunnable implements Runnable { int i = 0; public MyRunnable(int i) { this.i = i; } @Override public void run() { try { System.out.print(i+" "); Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } }
运行的结果
0 1 2 3 4 5 6 7 8 9
按照顺序,一个一个一次执行。