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: ...
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 ...
**Item 22: Prefer Defining and Implementing Interfaces to Inheritance** - **Reasons:** Interfaces promote loose coupling and make it easier to swap implementations. - **Implementation:** ```csharp ...
在编写自定义的复制构造函数和赋值运算符时,还需要遵循一些最佳实践,如"拷贝-and-swap"策略(Copy-and-Swap Idiom)。这个策略在赋值运算符中先创建一个临时对象,然后交换这个临时对象和当前对象的成员,这样可以...
4. **特定情况**:`prefer to do sth.` 用于强调在特定情境下,更愿意做某事,例如:"I prefer to walk."(我宁愿走路。) 5. **对比形式**:`prefer doing sth. to doing sth.` 这种结构表示在两个动作之间,更...
2. **Item 7: Prefer Initialization to Assignment** - **核心观点**:提倡初始化而非赋值,尤其是在构造函数和成员变量的初始化过程中。 - **应用场景**:适用于对象创建时的资源管理,有助于减少内存泄漏和提高...
- "prefer to do" 表示更倾向于做某事,而 "prefer doing" 表示平时更喜欢做某事。 题目示例: 3. 题目"If he takes on this work, he will have no choice but ____________an even greater challenge.",这里使用...
3. prefer sth to sth,如:I prefer fish to meat. 或 I prefer reading to playing. 通过以上内容的学习,学生应能理解并运用这些词汇和语法结构,进行关于音乐喜好的交流,同时也能理解和使用定语从句,提升英语...
- 宁愿…也不…:prefer…to… - 儿童节目:children's program - 新闻报道:news report - 体育节目:sports program - 教育节目:educational program - 娱乐节目:entertainment program - 爱上…:fall ...
此外,还有一些不常接动名词但接不定式的动词,如:aim to do sth., fail to do sth., long to do sth., happen to do sth., hesitate to do sth., struggle to do sth. 二、接不定式作宾补的动词: 1. advise sb....
ESLint是针对javascript代码的静态检查工具。资源是本人配置的ESLint 规则文件。官方文档:http://eslint.org/docs/rules/。
- 宁愿某人做某事,例如:I prefer you to come early rather than late. 28. request sb. to do sth. - 要求某人做某事,例如:The client requested a meeting to discuss the project. 29. remind sb. to do sth...
* prefer to * compare to * contrast to 这些例句中,to表示比较或对比的意思,强调两者之间的差异或相似性。 五、tҏ 与 及 个 别 的 名 词 构 成 比 较 之 意 在这个用法中,to与及个别的名词构成比较之意。...
- 用`prefer...to...`改写:I prefer red apples to green ones. - 用`would rather...than...`改写:I would rather walk than ride a bike. - 与橘子相比,她更喜欢苹果:She prefers apples to oranges. - 我...
7. 题目考察固定搭配:prefer to do sth. rather than do sth. 意为“宁愿做…而不愿做…”,所以B. to stay 是正确答案。 8. 题目考察动词短语的搭配:borrow 意为“借入”,lend 意为“借出”,所以A. borrow, ...
- 更喜欢做某事:prefer to do sth. - 宁愿某人做某事:would rather sb. do sth. - 喜欢A而不喜欢B:like A better than B - 宁愿做...而不愿做...:would rather do... than do... - 感觉想做某事:feel like...
- "prefer + doing/to + doing":更喜欢做某事,例如:I prefer reading books to watching TV. - "go + comparative":表示进一步,例如:He wants to go further in his career. - "every other day/each two ...