什么是线程池?
不实用线程池的问题?
谁代表线程池?
如何得到线程池对象?
ThreadPoolExcutor构造器
创建代码演示如下:
public class ThreadPoolTest1 { public static void main(String[] args) { ExecutorService pool = new ThreadPoolExecutor(3,5,8, TimeUnit.SECONDS,new ArrayBlockingQueue<>(4), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy()); } }
线程池的注意事项
1.临时线程什么时候创建?
2.什么时候开始拒绝新任务?
ExecutorService的常用方法
新任务拒绝策略
代码演示如下:
public class MyRunnable implements Runnable{ @Override public void run() { System.out.println(Thread.currentThread().getName() + "===> 输出666~"); try { Thread.sleep(Integer.MAX_VALUE); } catch (Exception e) { e.printStackTrace(); } } }
public class ThreadPoolTest1 { public static void main(String[] args) { // 通过ThreadPoolExecutor创建一个线程池对象 ExecutorService pool = new ThreadPoolExecutor(3, 5, 8, TimeUnit.SECONDS, new ArrayBlockingQueue<>(4), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy()); Runnable target = new MyRunnable(); pool.execute(target);// 线程池会自动创建一个新线程,自动处理这个任务,自动执行的! pool.execute(target);// 线程池会自动创建一个新线程,自动处理这个任务,自动执行的! pool.execute(target);// 线程池会自动创建一个新线程,自动处理这个任务,自动执行的! pool.execute(target); pool.execute(target); pool.execute(target); pool.execute(target); // 到了临时线程的创建时机了 pool.execute(target); pool.execute(target); // 到了新任务拒绝时机了 pool.execute(target); //pool.shutdown(); // 等着线程池的任务全部执行完毕后,再关闭线程池 //pool.shutdownNow(); // 立即关闭线程池!不管任务是否执行完毕! } }
线程池处理Callable接口
代码演示如下:
public class MyCallable implements Callable{ private int n; public MyCallable(int n){ this.n = n; } @Override public String call() throws Exception { // 描述线程的任务,返回线程执行返回后的结果 int sum = 0; for (int i = 0; i <= n; i++) { sum += i; } return Thread.currentThread().getName() + "求出了1-" + n + "的和是:" + sum; } }
public class ThreadPoolTest2 { public static void main(String[] args) throws Exception{ // 1.通过ThreadPoolExecutor创建一个线程池对象 ExecutorService pool = new ThreadPoolExecutor(3, 5, 8, TimeUnit.SECONDS, new ArrayBlockingQueue<>(4), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy()); // 2.使用线程处理Callable任务。 Futuref1 = pool.submit(new MyCallable(100)); Future f2 = pool.submit(new MyCallable(200)); Future f3 = pool.submit(new MyCallable(300)); Future f4 = pool.submit(new MyCallable(400)); System.out.println(f1.get()); System.out.println(f2.get()); System.out.println(f3.get()); System.out.println(f4.get()); pool.shutdown(); } }
使用Executors得到线程池
注意:这些方法的底层,都是通过线程池的实现类ThreadPoolExecutor创建的线程池对象。
代码演示如下:
public class ThreadPoolTest3 { public static void main(String[] args) throws Exception{ // 1.通过ThreadPoolExecutor创建一个线程池对象 // ExecutorService pool = new ThreadPoolExecutor(3, 5, 8, // TimeUnit.SECONDS, new ArrayBlockingQueue<>(4), Executors.defaultThreadFactory(), // new ThreadPoolExecutor.AbortPolicy()); //1-2.通过Executors创建一个线程池对象 ExecutorService pool = Executors.newFixedThreadPool(13); //核心线程池数量到底配置多少? //计算密集型的任务:核心线程数量 = CPU的核数 + 1 //IO密集型的任务:核心线程数量 = CPU核数 * 2 // 2.使用线程处理Callable任务。 Futuref1 = pool.submit(new MyCallable(100)); Future f2 = pool.submit(new MyCallable(200)); Future f3 = pool.submit(new MyCallable(300)); Future f4 = pool.submit(new MyCallable(400)); System.out.println(f1.get()); System.out.println(f2.get()); System.out.println(f3.get()); System.out.println(f4.get()); pool.shutdown(); } }
Executors使用可能存在的陷阱