Guideline for effective use of thread 102 pools 16 Don't queue tasks that wait synchronously for results from other tasks Be careful when using pooled threads for potentially long-lived operations Understand your tasks.To tune the thread pool size effectively,you need to understand the tasks that are being queued and what they are doing. Institute of Computer Software 2022-2-27 Nanjing University
Guideline for effective use of thread pools Don 't queue tasks that wait synchronously for results from other tasks Be careful when using pooled threads for potentially long-lived operations Understand your tasks. To tune the thread pool size effectively, you need to understand the tasks that are being queued and what they are doing. 2022-2-27 Institute of Computer Software Nanjing University 16
No need to write your own 1902 17 java.util.concurrent,* ▣Executor framework ■Executor框架组件提供了一个简单的、标准的、可扩充 的类。该框架组件使调用、调度和执行的操作标准化了。 它通过一组执行策略为控制异步事务提供了支持。 ■Executor:接口执行已提交的可以运行的事务。它提供了 一条途径,允许我们把事务提交从事务执行机制中分离 出来。程序员通常使用Executor代替显式地(explicitly)建 立线程。Executor接口也提供事务的同步和异步执行。 Institute of Computer Software 2022-2-27 Nanjing University
No need to write your own java.util.concurrent.* Executor framework Executor框架组件提供了一个简单的、标准的、可扩充 的类。该框架组件使调用、调度和执行的操作标准化了。 它通过一组执行策略为控制异步事务提供了支持。 Executor接口执行已提交的可以运行的事务。它提供了 一条途径,允许我们把事务提交从事务执行机制中分离 出来。程序员通常使用Executor代替显式地(explicitly)建 立线程。Executor接口也提供事务的同步和异步执行。 2022-2-27 Institute of Computer Software Nanjing University 17
Executors Framework for asynchronous execution Standardize asynchronous invocation Separate submission from execution policy -use anExecutor.execute (aRunnable) -not new Thread(aRunnable).start() Two styles supported: -Actions:Runnables Functions (indirectly):Callables Also cancellation and shutdown support Usually created via Executors factory class Configures flexible ThreadPoolExecutor Customize shutdown methods,before/after hooks, saturation policies,queuing
2022-2-27 Institute of Computer Software Nanjing University 18
使用Executor:接口 19 public interface Executor 口执行已提交的Runnable任务的对象。此接口提供一 种将任务提交与每个任务将如何运行的机制(包括 线程使用的细节、调度等)分离开来的方法。通常 使用Executor而不是显式地创建线程。例如: Executor executor anExecutor; executor.execute(new RunnableTask1()); executor.execute(new RunnableTask2()); Institute of Computer Software 2022-2-27 Nanjing University
使用Executor接口 public interface Executor 执行已提交的 Runnable 任务的对象。此接口提供一 种将任务提交与每个任务将如何运行的机制(包括 线程使用的细节、调度等)分离开来的方法。通常 使用 Executor 而不是显式地创建线程。例如: Executor executor = anExecutor; executor.execute(new RunnableTask1()); executor.execute(new RunnableTask2()); 2022-2-27 Institute of Computer Software Nanjing University 19
&雪扇 实现Executor接口:立即执行 20 Class DirectExecutor implements Executor{ public void execute(Runnable r){ r.run(); } Institute of Computer Software 2022-2-27 Nanjing University
实现Executor接口:立即执行 Class DirectExecutor implements Executor{ public void execute(Runnable r) { r.run(); } } 2022-2-27 Institute of Computer Software Nanjing University 20