Java 线程池的基本操作
package com.zhong.thread.threadpool;import java.util.concurrent.*;/*** @ClassName : ThreadPool* @Description : 线程池的基本操作* @Author : zhx* @Date: 2024-02-19 18:03*/
public class ThreadPool {public static void main(String[] args) {// 创建线程池对象ExecutorService pool = new ThreadPoolExecutor(3,5,8,TimeUnit.SECONDS,new ArrayBlockingQueue<>(4),Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());Runnable r = new myRunnable();Thread t1 = new Thread(r);pool.execute(t1); // 线程池会自动创建一个线程执行这个任务pool.execute(t1); // 线程池会自动创建一个线程执行这个任务pool.execute(t1); // 线程池会自动创建一个线程执行这个任务pool.execute(t1); // 核心线程达到 3 服用前面的核心线程pool.execute(t1); // 核心线程达到 3 服用前面的核心线程pool.execute(t1);// 等线程池任务执行完毕后关闭pool.shutdown();// 直接关闭 并 返回没有执行完的线程 会抛异常pool.shutdownNow();}
}
class myRunnable implements Runnable {@Overridepublic void run() {System.out.println(Thread.currentThread().getName() + " 红红火火恍恍惚惚");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}
}
package com.zhong.thread.threadpool;import java.util.concurrent.*;/*** @ClassName : ThreadPool* @Description : 线程池的基本操作* @Author : zhx* @Date: 2024-02-19 18:03*/
public class ThreadPool {public static void main(String[] args) {// 创建线程池对象ExecutorService pool = new ThreadPoolExecutor(3,5,8,TimeUnit.SECONDS,new ArrayBlockingQueue<>(4),Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());Runnable r = new myRunnable();Thread t1 = new Thread(r);pool.execute(t1); // 线程池会自动创建一个线程执行这个任务pool.execute(t1); // 线程池会自动创建一个线程执行这个任务pool.execute(t1); // 线程池会自动创建一个线程执行这个任务pool.execute(t1); // 核心线程达到 3 复用前面的核心线程pool.execute(t1); // 核心线程达到 3 复用前面的核心线程pool.execute(t1);pool.execute(t1);pool.execute(t1); // 超过最大线程队伍队列 8>4+3 创建新的线程池 现在一共有 4 个线程池pool.execute(t1); // 超过最大线程队伍队列 9>4+3 创建新的线程池 现在一共有 5 个线程池// 已经占满 到了新任务的拒绝时机了 会抛出异常pool.execute(t1);}
}
class myRunnable implements Runnable {@Overridepublic void run() {System.out.println(Thread.currentThread().getName() + " 红红火火恍恍惚惚");try {
// Thread.sleep(1000);Thread.sleep(Integer.MAX_VALUE);} catch (InterruptedException e) {throw new RuntimeException(e);}}
}
package com.zhong.thread.threadpool;import java.util.concurrent.*;/*** @ClassName : ThreadPool* @Description : 线程池的基本操作* @Author : zhx* @Date: 2024-02-19 18:03*/
public class ThreadPool {public static void main(String[] args) throws ExecutionException, InterruptedException {// 创建线程池对象ExecutorService pool = new ThreadPoolExecutor(3,5,8,TimeUnit.SECONDS,new ArrayBlockingQueue<>(4),Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());Future<String> f1 = pool.submit(new myCallable(100));Future<String> f2 = pool.submit(new myCallable(200));Future<String> f3 = pool.submit(new myCallable(300));Future<String> f4 = pool.submit(new myCallable(400));Future<String> f5 = pool.submit(new myCallable(500));Future<String> f6 = pool.submit(new myCallable(600));System.out.println(f1.get());System.out.println(f2.get());System.out.println(f3.get());System.out.println(f4.get());System.out.println(f5.get());System.out.println(f6.get());}
}/*** @ClassName : myCallable* @Description : 创建线程类方法三 实现 Callable 接口返回 1-n 的和* @Author : zhx* @Date: 2024-02-19 11:26*/class myCallable implements Callable<String> {private int n;public myCallable(int n) {this.n = n;}// 重写 call() 方法@Overridepublic String call() throws Exception {int sum = 0;for (int i = 0; i <= n; i++) {sum += i;}return Thread.currentThread().getName() + "实现了求 1-" + n + " 的值是:" + (sum);}}