闲话少叙...
package com.adao.thread; import java.util.Calendar; import java.util.Date; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class ThreadPool { /** * 1.创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程 */ public static void cachedThreadPool() { ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); for (int j = 0; j < 10; j++) { for (int i = 0; i < 10; i++) { final int index = i; // try { // Thread.sleep(1000); // } catch (InterruptedException e) { // e.printStackTrace(); // } cachedThreadPool.execute(new Runnable() { @Override public void run() { System.out.println(index + "--" + Thread.currentThread().getId() + " name:" + Thread.currentThread().getName()); } }); } } } /** * 2.创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待 */ public static void newFixedThreadPool() { ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5); for (int j = 0; j < 10; j++) { for (int i = 0; i < 10; i++) { final int index = i; fixedThreadPool.execute(new Runnable() { @Override public void run() { try { System.out.println(index + "----" + Thread.currentThread().getId() + " name:" + Thread.currentThread().getName()); Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } }); } } // 两种关闭方式 // fixedThreadPool.shutdown(); //不会立即终止线程池,而是要等所有任务缓存队列中的任务都执行完后才终止,但再也不会接受新的任务 // fixedThreadPool.shutdownNow(); //立即终止线程池,并尝试打断正在执行的任务,并且清空任务缓存队列,返回尚未执行的任务 } /** * 3.创建一个定长线程池,支持定时及周期性任务执行 */ public static void newScheduledThreadPool() { ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(50); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 10); // 时 calendar.set(Calendar.MINUTE, 53);// 分 calendar.set(Calendar.SECOND, 00); // 秒 // 计算现在时间和计划任务执行时间差多久,单位毫秒 Long date = calendar.getTime().getTime() - System.currentTimeMillis(); // //延迟3秒执行 scheduledThreadPool.schedule(new Runnable() { @Override public void run() { System.out.println("delay 3 seconds--" + new Date().toLocaleString() + " name:" + Thread.currentThread().getName()); } }, 3, TimeUnit.SECONDS); // 延迟5秒执行,然后每隔2秒执行一次 scheduledThreadPool.scheduleAtFixedRate(new Runnable() { @Override public void run() { System.out.println("延迟5秒执行,然后每隔2秒执行一次--" + new Date().toLocaleString() + " name:" + Thread.currentThread().getName()); } }, 5, 2, TimeUnit.SECONDS); // 定时在某一时刻执行任务,然后间隔执行,如果时间过了会立马执行 scheduledThreadPool.scheduleAtFixedRate(new Runnable() { @Override public void run() { System.out.println("定时在某一时刻执行任务,然后间隔执行--" + new Date().toLocaleString() + " name:" + Thread.currentThread().getName()); } }, date / 1000, 2, TimeUnit.SECONDS); } /** * 4. 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务, 保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行 */ public static void newSingleThreadExecutor() { ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); for (int i = 0; i < 10; i++) { final int index = i; singleThreadExecutor.execute(new Runnable() { @Override public void run() { try { System.out.println("单线程执行任务。。。" + Thread.currentThread().getName()); Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } }); } } public static void main(String[] args) { // cachedThreadPool(); // newFixedThreadPool(); // newScheduledThreadPool(); newSingleThreadExecutor(); } }
执行结果:
newSingleThreadExecutor();
newFixedThreadPool();
细心的人可以观察到:
Thread.currentThread().getId() 和 Thread.currentThread().getName()的关系。
perfect