ThreadPoolExecutor 的使用详解

本文详细介绍了线程池ThreadPoolExecutor的使用方法及注意事项,包括构造方法参数解释、任务执行流程和优先级说明,并提供了具体的应用实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.使用线程池  ThreadPoolExecutor
构造方法:ThreadPoolExecutor(int corePoolSize, int maxinumPoolSize, long keepAliveTime, TimeUnit unit,
BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler)
 
 corePoolSize:线程池维护线程的最少数量
 maxinumPoolSize:线程池维护线程的最大数量
 keepAliveTime:线程池维护线程所允许的空闲时间
 unit:线程池维护线程所允许的空闲时间的单位
 workQueue:线程池所使用的缓冲队列
 handler:线程池对拒绝任务的处理策略
 
 
2.需要注意以下几点:
 
 (1)一个任务通过execute(Runnable)方法被添加到线程池,任务就是一个Runnable类型的对象,
  任务队的执行方法就是Runnable类型对象的run()方法。
 (2)当一个任务通过execute(Runnable)方法欲添加到线程池时;
  ①如果此时线程池中的线程的数量小于corePoolSize,即使线程池中的线程都处于空闲状态,也要创建新的线程
  来处理被添加的任务。
  ②如果此时线程池中的线程的数量等于corePoolSize,但是缓冲队列workQueue未满,那么任务被放入缓冲队列。
  ③如果此时线程池中的线程的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的线程数量小于
  maxinumPoolSize,创建新的线程来处理被添加的任务。
  ④如果此时线程池中的线程的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的线程数量等于
  maxinumPoolSize,那么通过handler所制定的策略来处理此任务。也就是:处理任务的优先级为:核心线程
  corePoolSize,任务队列workQueue,最大线程maxinumPoolSize,如果三者都满了,使用handler

  处理被拒绝的任务。


3.使用实例:

创建类 实现Runnable

public class EmailSender implements Runnable{
	//Shift + Alt + S + V  重写从父类继承过来的方法
	@Override
	public void run() {
//		System.out.println("发送数据");
		try {
			Thread.currentThread();
			Thread.sleep(Math.round(Math.random()*0.5*1000));
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
}
主方法:
<span style="white-space:pre">		</span>ThreadPoolExecutor execute = new ThreadPoolExecutor(50, 
				100, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10000));
		for (int i = 0; i < 10000; i++) {
			EmailSender sender = new EmailSender();
			execute.execute(sender);
		}
		//释放资源
		execute.shutdown();
		//监控 
		while(!execute.isTerminated()){
			execute.awaitTermination(1, TimeUnit.SECONDS);
			int process = Math.round((execute.getCompletedTaskCount() * 100)/execute.getTaskCount());
			System.out.println(process + "%," + execute.getCompletedTaskCount() + "发送完毕。");
		}

实现Callable<V>


public class EmailSenderC implements Callable<Boolean>{
	//Shift + Alt + S + V  重写从父类继承过来的方法
	@Override
	public Boolean call() throws Exception{
//		System.out.println("发送数据");
		try {
			Thread.currentThread();
			Thread.sleep(Math.round(Math.random()*0.5*1000));
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		//模拟成功率
		if(Math.random() > 0.7){
			return false;
		}
		return true;
	}
}

<span style="white-space:pre">		</span>ThreadPoolExecutor execute = new ThreadPoolExecutor(50, 
				100, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10000));
		List<Future<Boolean>> futures = new ArrayList<Future<Boolean>>();
		for (int i = 0; i < 10000; i++) {
			EmailSenderC sender = new EmailSenderC(); 
			Future<Boolean> submit = execute.submit(sender);
			futures.add(submit);
		}
		//释放资源
		execute.shutdown();
		//监控 
		while(!execute.isTerminated()){
			execute.awaitTermination(1, TimeUnit.SECONDS);
			int process = Math.round((execute.getCompletedTaskCount() * 100)/execute.getTaskCount());
			System.out.println(process + "%," + execute.getCompletedTaskCount() + "发送完毕。");
		}
		//统计
		int succeesCount = 0;
		int failureCount = 0;
		for (Future<Boolean> future : futures) {
			if (future.get()) {
				succeesCount++;
			}else{
				failureCount++;
			}
		}
		System.out.println("成功:"+ succeesCount + ",失败:" + failureCount);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值