By using executor service, you can:
1. wait for a particular task to complete
2. wait for any or all of a collection of tasks to complete
3. wait for executor service's graceful termination to complete
4. retrieve the results of tasks one by one as they complete
Choosing the executor service for a particular application can be tricky.
If you're writing a small program, or a lightly loaded server, using Executors.newCachedThreadPool is generally a good choice, as it demands no configuration and generally "does the right thing".
In a heavily loaded production server, you are much better off using Executors.newFixedThreadPool, which gives you a pool with a fixed number of threads, or using the ThreadPoolExecutor class directly, for maximum control.
Not only should you refrain from writing your own work queues, but you should generally refrain from working directly with threads. The key abstraction is no longer Thread, which served as both the unit of work and the mechanism for executing it. Now the unit of work and mechanishm are separate. The key abstraction is the unit of work, which is called a task. There are two kinds of tasks: Runnable and its close cousin, Callable. The general mechanism for executing tasks is the executor serivce. If you think in terms of tasks and let an executor service execute them for you, you gain great flexibility in term of selecting appropriate execution policies. In essence, the Executor Framework does for execution what the Collections Framework did for aggregation.
1. wait for a particular task to complete
2. wait for any or all of a collection of tasks to complete
3. wait for executor service's graceful termination to complete
4. retrieve the results of tasks one by one as they complete
Choosing the executor service for a particular application can be tricky.
If you're writing a small program, or a lightly loaded server, using Executors.newCachedThreadPool is generally a good choice, as it demands no configuration and generally "does the right thing".
In a heavily loaded production server, you are much better off using Executors.newFixedThreadPool, which gives you a pool with a fixed number of threads, or using the ThreadPoolExecutor class directly, for maximum control.
Not only should you refrain from writing your own work queues, but you should generally refrain from working directly with threads. The key abstraction is no longer Thread, which served as both the unit of work and the mechanism for executing it. Now the unit of work and mechanishm are separate. The key abstraction is the unit of work, which is called a task. There are two kinds of tasks: Runnable and its close cousin, Callable. The general mechanism for executing tasks is the executor serivce. If you think in terms of tasks and let an executor service execute them for you, you gain great flexibility in term of selecting appropriate execution policies. In essence, the Executor Framework does for execution what the Collections Framework did for aggregation.
本文探讨了如何有效地使用Java的ExecutorService来管理线程池,包括任务执行、等待任务完成、优雅终止服务及任务结果的获取。重点介绍了在不同负载情况下的线程池选择策略,并阐述了如何通过分离任务执行与线程机制,实现更灵活的执行策略。
1407

被折叠的 条评论
为什么被折叠?



