package com.xinboedu.www.test; import java.util.concurrent.BlockingQueue; import java.util.concurrent.Executor; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; public class ExecutorUtil {//https://blog.csdn.net/jubaoquan/article/details/79198780 //自定义线程池: private static final int CPU_COUNT = Runtime.getRuntime() .availableProcessors();// 获取当前CPU最大的可执行线程数 private static final int CORE_POOL_SIZE = 2;//CPU_COUNT * 2;// 线程池中执行的线程数 private static final int MAXIMUM_POOL_SIZE = CORE_POOL_SIZE + 1;// 线程池可以缓存的线程最大数 private static final int KEEP_ALIVE = 1; // 缓存线程的存活时间数 private static final BlockingQueue<Runnable> sPoolWorkQueue = new LinkedBlockingQueue<Runnable>( 128); // 线程等待的队列 private static final ThreadFactory sThreadFactory = new ThreadFactory() { // 线程工厂,用于创建线程 private final AtomicInteger mCount = new AtomicInteger(1);// 线程安全的计数器 public Thread newThread(Runnable r) { return new Thread(r, "自定义线程" + mCount.getAndIncrement()); } }; public static final Executor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor( CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.MINUTES, sPoolWorkQueue, sThreadFactory); }
package com.xinboedu.www.test; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Test { public static void main(String[] args) { //1、创建一个同一时间只能执行一个任务的线程 // ExecutorService service = // Executors.newSingleThreadExecutor(); // MyRunnable r1 = new MyRunnable("任务1"); // MyRunnable r2 = new MyRunnable("任务2"); // MyRunnable r3 = new MyRunnable("任务3"); // MyRunnable r4 = new MyRunnable("任务4"); // MyRunnable r5 = new MyRunnable("任务5"); // MyRunnable r6 = new MyRunnable("任务6"); // service.execute(r1); // service.execute(r2); // service.execute(r3); // service.execute(r4); // service.execute(r5); // service.execute(r6); // service.shutdown(); //提交任务完关闭程序 //2、创建一个同一时间能执行指定个任务的线程 // ExecutorService service = // Executors.newFixedThreadPool(4); // MyRunnable r1 = new MyRunnable("任务1"); // MyRunnable r2 = new MyRunnable("任务2"); // MyRunnable r3 = new MyRunnable("任务3"); // MyRunnable r4 = new MyRunnable("任务4"); // MyRunnable r5 = new MyRunnable("任务5"); // MyRunnable r6 = new MyRunnable("任务6"); // service.execute(r1); // service.execute(r2); // service.execute(r3); // service.execute(r4); // service.execute(r5); // service.execute(r6); // service.shutdown(); //可以缓存线程的线程池 (60秒,在60秒内开启新线程会去复用已开启的线程) // ExecutorService service = // Executors.newCachedThreadPool(); // MyRunnable r1 = new MyRunnable("任务1"); // MyRunnable r2 = new MyRunnable("任务2"); // MyRunnable r3 = new MyRunnable("任务3"); // MyRunnable r4 = new MyRunnable("任务4"); // MyRunnable r5 = new MyRunnable("任务5"); // MyRunnable r6 = new MyRunnable("任务6"); // service.execute(r1); // service.execute(r2); // try // { // Thread.sleep(3000); // } catch (InterruptedException e) // { // // TODO Auto-generated catch block // e.printStackTrace(); // } // service.execute(r3); // service.execute(r4); // service.execute(r5); // service.execute(r6); // service.shutdown(); //提交任务完关闭程序 //4、 MyRunnable r1 = new MyRunnable("任务1"); MyRunnable r2 = new MyRunnable("任务2"); MyRunnable r3 = new MyRunnable("任务3"); MyRunnable r4 = new MyRunnable("任务4"); MyRunnable r5 = new MyRunnable("任务5"); MyRunnable r6 = new MyRunnable("任务6"); ExecutorUtil.THREAD_POOL_EXECUTOR.execute(r1);//提交任务 ExecutorUtil.THREAD_POOL_EXECUTOR.execute(r2); ExecutorUtil.THREAD_POOL_EXECUTOR.execute(r3); ExecutorUtil.THREAD_POOL_EXECUTOR.execute(r4); ExecutorUtil.THREAD_POOL_EXECUTOR.execute(r5); ExecutorUtil.THREAD_POOL_EXECUTOR.execute(r6); ExecutorService service = (ExecutorService) ExecutorUtil.THREAD_POOL_EXECUTOR; service.shutdown();//借助向上转型关掉线程池 } } class MyRunnable implements Runnable { private String info; public MyRunnable(String info) { this.info = info; } @Override public void run() { System.out.println(Thread.currentThread().getName() + ":" + info ); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } }