如何关闭一个java线程池

Java 并发工具包中 java.util.concurrent.ExecutorService 接口定义了线程池任务提交、获取线程池状态、线程池停止的方法等。

JDK 1.8 中,线程池的停止一般使用 shutdown()、shutdownNow()方法。

shutdown

  • 有序关闭,已提交任务继续执行
  • 不接受新任务

主线程向线程池提交了 10 个任务,休眠 4 秒后关闭线程池,线程池把 10 个任务都执行完成后关闭了。

public static void main(String[] args) {
		//创建固定 3 个线程的线程池
		ExecutorService threadPool = Executors.newFixedThreadPool(3);
		
		//向线程池提交 10 个任务
		for (int i = 1; i <= 10; i++) {
			final int index = i;
			threadPool.submit(() -> {
				System.out.println("正在执行任务 " + index);
				//休眠 3 秒
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			});
		}
		
		//休眠 4 秒
		try {
			Thread.sleep(4000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
 
		//关闭线程池
		threadPool.shutdown();
	}

 

shutdownNow

  • 尝试停止所有正在执行的任务
  • 停止等待执行的任务,并返回等待执行的任务列表

主线程向线程池提交了 10 个任务,休眠 4 秒后关闭线程池,线程池执行了数个任务后抛出异常,打印返回的剩余未执行的任务个数。

public static void main(String[] args) {
		//创建固定 3 个线程的线程池
		ExecutorService threadPool = Executors.newFixedThreadPool(3);
		
		//向线程池提交 10 个任务
		for (int i = 1; i <= 10; i++) {
			final int index = i;
			threadPool.submit(() -> {
				System.out.println("正在执行任务 " + index);
				//休眠 3 秒
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			});
		}
		
		//休眠 4 秒
		try {
			Thread.sleep(4000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
 
		//关闭线程池
		List tasks = threadPool.shutdownNow();
		System.out.println("剩余 " + tasks.size() + " 个任务未执行");
}

判断线程池是否关闭awaitTermination

  • 收到关闭请求后,所有任务执行完成、超时、线程被打断,阻塞直到三种情况任意一种发生
  • 参数可以设置超时时间于超时单位
  • 线程池关闭返回 true;超过设置时间未关闭,返回 false
public static void main(String[] args) {
		//创建固定 3 个线程的线程池
		ExecutorService threadPool = Executors.newFixedThreadPool(3);
		
		//向线程池提交 10 个任务
		for (int i = 1; i <= 10; i++) {
			final int index = i;
			threadPool.submit(() -> {
				System.out.println("正在执行任务 " + index);
				//休眠 3 秒
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			});
		}
		
		//关闭线程池,设置等待超时时间 3 秒
		System.out.println("设置线程池关闭,等待 3 秒...");
		threadPool.shutdown();
		try {
			boolean isTermination = threadPool.awaitTermination(3, TimeUnit.SECONDS);
			System.out.println(isTermination ? "线程池已停止" : "线程池未停止");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		//再等待超时时间 20 秒
		System.out.println("再等待 20 秒...");
		try {
			boolean isTermination = threadPool.awaitTermination(20, TimeUnit.SECONDS);
			System.out.println(isTermination ? "线程池已停止" : "线程池未停止");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

shutdown和shutdownNow的异同

  • 调用 shutdown() 和 shutdownNow() 方法关闭线程池,线程池都无法接收新的任务。
  • shutdown() 方法会继续执行正在执行未完成的任务,shutdownNow() 方法会尝试停止所有正在执行的任务。
  • shutdown() 方法没有返回值,shutdownNow() 方法返回等待执行的任务列表。
  • awaitTermination(long timeout, TimeUnit unit) 方法可以获取线程池是否已经关闭,需要配合 shutdown() 使用。
  • shutdownNow() 不一定能够立马结束线程池,该方法会尝试停止所有正在执行的任务,通过调用 Thread.interrupt() 方法来实现的,如果线程中没有 sleep() 、wait()、Condition、定时锁等应用,interrupt() 方法是无法中断当前的线程的。

你可能感兴趣的:(如何关闭一个java线程池)