`
z_wang
  • 浏览: 4916 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

Prefer executors and tasks to threads

    博客分类:
  • Java
 
阅读更多
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.
分享到:
评论

相关推荐

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    Item 80: Prefer executors, tasks, and streams to threads Item 81: Prefer concurrency utilities to wait and notify Item 82: Document thread safety Item 83: Use lazy initialization judiciously Item 84: ...

    专享:prefer的用法__(全).pdf

    13. “I would prefer to spend the weekend at home rather than driving all the way to your mother’s.”说话者宁愿在家度过周末,而不愿长途驾车去对方母亲家。 14. “I should prefer beef rather than ...

    I_would_prefer_not_to

    "I_would_prefer_not_to"这个标题可能暗示着某种不愿意或不情愿,但在HTML的世界里,我们可以理解为对特定技术或者实现方式的偏好。 首先,HTML的基本结构由一系列元素(tags)组成,这些元素通过尖括号包围,如`...

    SONY 索尼 Phantom LUTs - A7s3-Fx3 FX6 全套

    Hi there, First of all, thanks for purchasing my LUTs - I hope they help you unlock the full ...I like efficiency and prefer products and services that just ‘work’ without a tonne of tweaking and effor

    新人教九年级英语unit单词讲解PPT课件.pptx

    4. **特定情况**:`prefer to do sth.` 用于强调在特定情境下,更愿意做某事,例如:"I prefer to walk."(我宁愿走路。) 5. **对比形式**:`prefer doing sth. to doing sth.` 这种结构表示在两个动作之间,更...

    Building and Testing with Gradle

    This concise introduction provides numerous code examples to help you explore Gradle, both as a build tool and as a complete solution for automating the compilation, test, and release process of ...

    Mastering Apache Cassandra(PACKT,2013)

    If you are at an intermediate level, you may prefer to use this guide to help you dive into the architecture. To a DevOp, this book will help you manage and optimize your infrastructure. To a CTO, ...

    网络协议攻击

    can use the example application to learn the skills of protocol analysis and attack the sample network protocols. Here is a brief breakdown of each chapter: Chapter 1: The Basics of Networking This ...

    core_servlets_and_javaserver_pages_advanced_technologies_volume_2_2nd_edition

    You want to customize your site for visitors’ prefer- ences and interests, but how? You want to keep track of user’s purchases as they shop at your site, but what techniques are required to ...

    Pro SQL Server Internals

    Improve your ability to develop, manage, and ... This book is an excellent choice for people who prefer to understand and fix the root cause of a problem rather than applying a 'band aid' to it.

    2013年九年级英语下册 Module 4 The way we look Unit 2 What helps you choo

    "the same...as", "the number of", "care about", "look cool", "prefer...to...", "above all", and "because of". Show examples and ask students to use these words in sentences to reinforce understanding...

    ActiveState Komodo IDE 10.2.1.89853 Setup + Keygen

    Gulp, Grunt and NPM tasks. The integration allows you to run commands for those task runners in your shell scope (inside Commando), it also adds a button to your dynamic toolbar (new in Komodo 10) ...

    介词to的用法归纳.pdf

    * prefer to * compare to * contrast to 这些例句中,to表示比较或对比的意思,强调两者之间的差异或相似性。 五、tҏ 与 及 个 别 的 名 词 构 成 比 较 之 意 在这个用法中,to与及个别的名词构成比较之意。...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Therefore, we prefer to minimize includes, particularly includes of header files in other header files. You can significantly minimize the number of header files you need to include in your own ...

Global site tag (gtag.js) - Google Analytics