在写程序时有些异步程序只执行一遍就不需要了,为了方便经常会写下面的代码
new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub } }).start();这样new出来的匿名对象会存在一些问题
/** * 可以缓存线程池 */ public static void Function1() { ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < 50; i++) { final int index = i; try { Thread.sleep(100); // 休眠时间越短创建的线程数越多 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } executorService.execute(new Runnable() { @Override public void run() { // TODO Auto-generated method stub System.out.println("active count = " + Thread.activeCount() + " index = " + index); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } }打印结果
/** * 定长线程池 */ public static void Function2() { ExecutorService executorService = Executors.newFixedThreadPool(3); for (int i = 0; i < 30; i++) { final int index = i; executorService.execute(new Runnable() { @Override public void run() { try { System.out.println("index = " + index + " thread count = " + Thread.activeCount()); Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } }3.newScheduledThreadPool
/** * 定长线程池,可做延时 */ public static void Function3() { ScheduledExecutorService executorService = Executors .newScheduledThreadPool(5); executorService.schedule(new Runnable() { @Override public void run() { System.out.println("delay 3 seconds" + " thread count = " + Thread.activeCount()); } }, 3, TimeUnit.SECONDS); } /** * 定期执行,可以用来做定时器 */ public static void Function4() { ScheduledExecutorService executorService = Executors .newScheduledThreadPool(3); executorService.scheduleAtFixedRate(new Runnable() { @Override public void run() { System.out .println("delay 1 seconds, and excute every 3 seconds" + " thread count = " + Thread.activeCount()); } }, 1, 3, TimeUnit.SECONDS); }打印结果
delay 1 seconds, and excute every 3 seconds thread count = 2 delay 1 seconds, and excute every 3 seconds thread count = 3 delay 1 seconds, and excute every 3 seconds thread count = 4 delay 1 seconds, and excute every 3 seconds thread count = 4 delay 1 seconds, and excute every 3 seconds thread count = 4 delay 1 seconds, and excute every 3 seconds thread count = 4 delay 1 seconds, and excute every 3 seconds thread count = 4 delay 1 seconds, and excute every 3 seconds thread count = 4 delay 1 seconds, and excute every 3 seconds thread count = 4
4.newSingleThreadExecutor这个最简单
/** * 单例线程 */ public static void Function5() { ExecutorService singleThreadExecutor = Executors .newSingleThreadExecutor(); for (int i = 0; i < 5; i++) { final int index = i; singleThreadExecutor.execute(new Runnable() { @Override public void run() { try { System.out.println("index = " + index + " thread count = " + Thread.activeCount()); Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } }打印结果:
index = 0 thread count = 2 index = 1 thread count = 2 index = 2 thread count = 2 index = 3 thread count = 2 index = 4 thread count = 2只创建了一个线程