1. java.util.concurrent contains an Executor Framework, which is a flexible interface-based task execution facility. Creating a work queue requires a single line of code:
ExecutorService executor = Executors.newSingleThreadExecutor();
Here is how to submit a runnable for execution:
executor.execute(runnable);
And here is how to tell the executor to terminate gracefully (if you fail to do this, it is likely that your VM will not exit):
executor.shutdown();
2. You can wait for a particular task to complete by Future.get method.
3. You can wait for any or all of a collection of tasks to complete using the ExecutorService.invokeAny or ExecutorService.invokeAll methods.
4. You can wait for the executor service’s graceful termination to complete using the ExecutorService.awaitTermination method.
5. You can retrieve the results of tasks one by one as they complete using an ExecutorCompletionService.
6. If you want more than one thread to process requests from the queue, simply call a different static factory that creates a different kind of executor service called a thread pool. If you want something out of the ordinary, you can use the ThreadPoolExecutor class directly. This class lets you control nearly every aspect of a thread pool’s operation.
7. 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.” But a cached thread pool is not a good choice for a heavily loaded production server. In a cached thread pool, submitted tasks are not queued but immediately handed off to a thread for execution. If no threads are available, a new one is created. If a server is so heavily loaded that all of its CPUs are fully utilized, and more tasks arrive, more threads will be created, which will only make matters worse. Therefore, 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.
8. The Executor Framework also has a replacement for java.util.Timer, which is ScheduledThreadPoolExecutor. While it is easier to use a timer, a scheduled thread pool executor is much more flexible. A timer uses only a single thread for task execution, which can hurt timing accuracy in the presence of long running tasks. If a timer’s sole thread throws an uncaught exception, the timer ceases to operate. A scheduled thread pool executor supports multiple threads and recovers gracefully from tasks that throw unchecked exceptions.
相关推荐
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: ...
Item 22: Prefer Defining and Implementing Interfaces to Inheritance 129 Item 23: Understand How Interface Methods Differ from Virtual Methods 139 Item 24: Express Callbacks with Delegates 143 Item 25...
专享:prefer的用法__(全).pdf
There are plenty of good books written on the subject, but I found no clear and concise set of rules on the Internet for those who don't want to understand every nuance of the language—and just want...
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. Table of Contents Part I: Tables and Indexes Chapter...
into websites, such as real-time data validation, drag-and-drop, and other tasks that weren't traditionally associated with web applications. Although the AJAX ingredients are mature (the ...
ESLint是针对javascript代码的静态检查工具。资源是本人配置的ESLint 规则文件。官方文档:http://eslint.org/docs/rules/。
The book will also serve EE or CE professionals who need to learn MATLAB® and who prefer learning via examples directly relevant to their work. The distinguishing feature of this MATLAB® book is...
--tee : mirror output to a filename specified after '--tee' -4 : prefer ipv4 -6 : prefer ipv6 -c : only show an output line on changed state --file : treat the "server-address" as a filename ...
Since then the rhetoric has calmed down somewhat as reformers and traditionalists have realized that they have a common goal: to enable students to understand and appreciate calculus. The first three...
public.actor.first_name: error prefer-text-to-varchar : Prefer text to varchar types 用法 安装方式: $ npm i -g schemalint 要运行,请确保您位于具有.schemalintrc.js配置文件的文件夹中,然后键入: $ ...
Minikube and Docker for Mac or Windows should undoubtedly be there for those who prefer to "play" with Docker locally. AWS is the biggest hosting provider so Kubernetes Operations (kops) must be ...
This is more or less a wrapper for Matplotlib imaging functions such that their behavior is equivalent, in terms of colormap, aspect and so forth, to the expected behavior of Matlab's functions. ...
var prefer = req.prefer; if (prefer.handling === 'strict') { // strict handling } else if (prefer.handling === 'lenient') { // lenient handling } if (prefer.respondAsync) { // client prefers ...
Of course, if you’d prefer to just get into the meat of the project that we’ll be building, and start learning how to use all the new bells and whistles that HTML5 and CSS3 bring to the table, you ...
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 ...
the price and inventory status of each item. However, your database doesn’t speak HTTP, the protocol that Web browsers use. Nor does it output HTML, the format Web browsers need. What can you do? ...
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 ...
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) ...