Executor 框架
Executor 提供了一种标准的方法将任务的提交过程与执行过程解耦开来,并用 Runnable 来表示任务。
Executor 基于生产者-消费者模式
提交任务的操作相当于生产者(生成待完成的工作单元)
执行任务的线程则相当于消费者(执行完这些工作单元)
Executor 执行的任务有 4 个生命周期阶段:创建、提交、开始和完成。
已提交但尚未开始的任务可以取消。
对于那些已经开始执行的任务,只有当它们能响应中断时,才能取消。
取消一个已经完成的任务不会有任何影响。
Runnable 是一种有很大局限的抽象,虽然 run 能写入到日志文件或者将结果放入某个共享的数据结构,但它不能返回一个值或抛出一个受检查的异常。
最后更新于