java.util.concurrent学习之 Executor

首先我们先来看一下Executor的继承树关系:


然后了解下各个接口或类的意思

Executor :最上层接口,里面只有execute的方法
ExecutorService : 继承Executor,提供了更多丰富的方法(submit,invokeAny,invokeAll),对ExecutorService的接口实现有两个(ThreadPoolExecutor,ScheduledThreadPoolExecutor)
				  这两个即是java线程池具体实现类。
AbstractExecutorService:ExecutorService执行方法的默认实现
ScheduledExecutorService:对其中的任务进行调度,比如延迟执行、定时执行等等
ScheduledThreadPoolExecutor:ScheduledExecutorService的实现,一个可定时调度任务的线程池
ThreadPoolExecutor:线程池,可以通过调用Executors以下静态工厂方法来创建线程池并返回一个ExecutorService对象
Executors :工厂类,创建(newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor)四种类型ExecutorService线程池。
		   它的方法返回的都是ThreadPoolExecutor、ScheduledThreadPoolExecutor这两个类的实例。

应用场景,一般用来起线程异步操作一些事情,应用代码

public class Test {
	public static void main (String[] args) throws Exception {

		ExecutorService fixedThreadPool = Executors.newFixedThreadPool(20);
		fixedThreadPool.execute(new Runnable() {
			public void run() {
					System.out.println("hahaha ");
			}
		});
		
		ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(10);
		//任务调度延迟执行,使用Callable可以返回数据
		ScheduledFuture<String> schedule = scheduledThreadPool.schedule(new Callable<String>() {
			public String call() throws Exception{
				System.out.println("hahahahah ");
				return "xixi";
			}
		}, 5, TimeUnit.SECONDS); 
		
		String str = schedule.get();
		System.out.println(str);
		 
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用java.util.concurrent.ThreadPoolExecutor测试的例子: ```java import java.util.concurrent.*; public class TestThreadPoolExecutor { public static void main(String[] args) { int corePoolSize = 2; int maxPoolSize = 4; long keepAliveTime = 10; TimeUnit unit = TimeUnit.SECONDS; BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(2); ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, unit, workQueue); executor.execute(new Task("任务1")); executor.execute(new Task("任务2")); executor.execute(new Task("任务3")); executor.execute(new Task("任务4")); executor.execute(new Task("任务5")); executor.shutdown(); } static class Task implements Runnable { private String name; public Task(String name) { this.name = name; } @Override public void run() { System.out.println(Thread.currentThread().getName() + " 正在执行 " + name); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " 执行 " + name + " 完成"); } } } ``` 输出结果: ``` pool-1-thread-1 正在执行 任务1 pool-1-thread-2 正在执行 任务2 pool-1-thread-1 执行 任务1 完成 pool-1-thread-2 执行 任务2 完成 pool-1-thread-3 正在执行 任务3 pool-1-thread-4 正在执行 任务4 pool-1-thread-3 执行 任务3 完成 pool-1-thread-4 执行 任务4 完成 Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task TestThreadPoolExecutor$Task@7f31245a rejected from java.util.concurrent.ThreadPoolExecutor@6d6f6e28[Running, pool size = 4, active threads = 4, queued tasks = 2, completed tasks = 0] at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063) at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830) at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379) at TestThreadPoolExecutor.main(TestThreadPoolExecutor.java:16) ``` 在这个例子中,我们创建了一个ThreadPoolExecutor对象,并向其中添加了5个任务。由于线程池的核心线程数为2,最大线程数为4,所以前两个任务会立即执行,而后面的三个任务会被放入阻塞队列中等待执行。由于阻塞队列的大小为2,所以当第5个任务被添加时,阻塞队列已满,此时线程池会创建新的线程来执行任务。由于线程池的最大线程数为4,所以当第6个任务被添加时,线程池已经达到最大线程数,此时会抛出RejectedExecutionException异常。最后,我们调用了线程池的shutdown()方法来关闭线程池。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值