java.util.concurrent源码学习系列--Executor

Executor的系列类图如下:这一些了类或接口都是和任务提交和执行相关的。

 


java.util.concurrent.Executor接口:

Java代码   收藏代码
  1.  /* @since 1.5 
  2.  * @author Doug Lea 
  3.  */  
  4. public interface Executor {  
  5.   
  6.     /** 
  7.      * Executes the given command at some time in the future.  The command 
  8.      * may execute in a new thread, in a pooled thread, or in the calling 
  9.      * thread, at the discretion of the <tt>Executor</tt> implementation. 
  10.      *会在将来某个时刻去执行任务,也就是所谓的异步执行 
  11.      * 
  12.      * @param command the runnable task 
  13.      * @throws RejectedExecutionException if this task cannot be 
  14.      * accepted for execution. 
  15.      * @throws NullPointerException if command is null 
  16.      */  
  17.     void execute(Runnable command);  
  18. }  

java.util.concurrent.ExecutorService接口,该接口定义的方法如下:



 这些方法一部分在AbstractExecutorService提供了默认的实现,一部分延迟到ThreadPoolExecutor里实现。

java.util.concurrent.AbstractExecutorService抽象类:

java.util.concurrent.ThreadPoolExecutor类:
  • 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、付费专栏及课程。

余额充值