> 建立固定大小的线程池java
> 建立缓存线程池缓存
> 建立单一线程池(如何实现线程死掉后重启?)并发
> shutdown与shutdownNow的比较
前者是任务执行完毕即关闭程序,或者表示当即关闭而不会关心任务是否已经完成。ide
> 调用ScheduledExecutorService的schedule方法,返回的ScheduleFuture对象能够取消任务oop
> 支持间隔重复任务的定时方式,不直接支持绝对定时方式,须要转换成相对时间方式spa
/** * @Title: ThreadPoolTest.java * @Package com.lh.threadtest.t8 * @Description: TODO * @author Liu * @date 2018年1月17日 下午2:17:40 * @version V1.0 */ package com.lh.threadtest.t8; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; /** * @ClassName: ThreadPoolTest * @Description: java5线程并发库的应用(常见几种线程池类型) * @author Liu * @date 2018年1月17日 下午2:17:40 * */ public class ThreadPoolTest { /*** * @Title: main * @Description: TODO * @param @param args * @return void * @throws */ public static void main(String[] args) { //建立固定大小的线程池 // ExecutorService executorService = Executors.newFixedThreadPool(3); //建立缓存线程池 // ExecutorService executorService = Executors.newCachedThreadPool(); //建立单一线程池(如何实现线程死掉后重启?) ExecutorService executorService = Executors.newSingleThreadExecutor(); for(int i = 1; i <= 10; i++){ final int task = i; executorService.execute(new Runnable() { @Override public void run() { for(int i = 1; i <= 10; i++){ try { //模拟处理延时 TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " is looping of: " + i + " for task of " + task); } } }); } System.out.println("All of 10 tasks have commited!"); // executorService.shutdownNow(); executorService.shutdown(); } }
/** * @Title: ThreadPoolTest.java * @Package com.lh.threadtest.t8 * @Description: TODO * @author Liu * @date 2018年1月17日 下午2:17:40 * @version V1.0 */ package com.lh.threadtest.t8; import java.util.Date; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; /** * @ClassName: ThreadPoolTest * @Description: java5线程并发库的应用(线程池定时任务) * * 支持间隔重复任务的定时方式,不直接支持绝对定时方式,须要转换成相对时间方式 * date.getTime() - System.currentTimeMillis() * * @author Liu * @date 2018年1月17日 下午2:17:40 * */ public class ThreadPoolTest2 { /*** * @Title: main * @Description: TODO * @param @param args * @return void * @throws */ public static void main(String[] args) { Executors.newScheduledThreadPool(3).schedule(new Runnable() { @Override public void run() { System.out.println("bombing!"); } }, 10, TimeUnit.SECONDS); while(true){ try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(new Date().getSeconds()); } } }
/** * @Title: ThreadPoolTest.java * @Package com.lh.threadtest.t8 * @Description: TODO * @author Liu * @date 2018年1月17日 下午2:17:40 * @version V1.0 */ package com.lh.threadtest.t8; import java.util.Date; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; /** * @ClassName: ThreadPoolTest * @Description: java5线程并发库的应用(定时任务,连环爆炸) * * 支持间隔重复任务的定时方式,不直接支持绝对定时方式,须要转换成相对时间方式: * date.getTime() - System.currentTimeMillis() * * @author Liu * @date 2018年1月17日 下午2:17:40 * */ public class ThreadPoolTest3 { /*** * @Title: main * @Description: java5线程并发库的应用 * @param @param args * @return void * @throws */ public static void main(String[] args) { Executors.newScheduledThreadPool(3).scheduleAtFixedRate(new Runnable() { @Override public void run() { System.out.println("bombing!"); } }, 5, 3, TimeUnit.SECONDS); while(true){ try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(new Date().getSeconds()); } } }