Java Notes-7
来源:互联网 发布:正在安装网络组件 编辑:程序博客网 时间:2024/05/17 06:26
of an executor service that manages tasks and that of a thread pool that services tasks in
an efficient way.
-The new Callable interface, which is effectively
a replacement for Runnable , rectifies this situation by providing a call() method that
both returns a result and can throw exceptions.
-The new Future class is used with Callable and serves as a handle to wait for and
retrieve the result of the task or cancel the task before it is executed.
-By
contrast, an ExecutorService is intended to be an asynchronous task handler. Instead
of an execute() method, it has submit() methods that accept a Callable (or Runna
ble ) and return immediately with a Future object that can be used to manage the task
and collect the result later
-newFixedThreadPool(int)
This is the classic thread pool with a specified maximum pool size and an unboun‐
ded queue for task submission. If a thread dies for some reason while handling a
task, a new one will be created to replace it. Threads are never removed from the
pool until the service is shut down.
newCachedThreadPool()
This pool uses an open-ended number of threads that grows and shrinks with de‐
mand. The main advantage of this service is that threads are cached for a period of
time and reused, eliminating the overhead of creating new threads for short-lived
tasks. Threads that are not used for one minute are removed. Tasks are submitted
directly to threads; there is no real queuing.
newSingleThreadExecutor()
This ExecutorService uses a single thread to execute tasks from an unbounded
queue. In this sense, it is identical to a fixed thread pool with a pool size of 1
-In addition to its individual task submit() methods, ExecutorService also offers a set
of collective invokeAll() and invokeAny() executor methods that submit multiple
tasks as a group and return results either when they are all complete or when the first
one completes, respectively.
-A CompletionService is a lightweight queue-like frontend to an executor. The Comple
tionService provides submit() methods, which delegate their tasks to a particular
instance of Executor , and then provides take() and poll() methods for retrieving
Future results for completed tasks.
-The Fork/Join framework is a new API added in Java 7 that provides just this—a way
for you to structure your tasks so that they can be split up as needed to keep all of the
available threads busy working on data with as much continuity as possible.
-The framework then
implements what is known as a “work stealing” algorithm, allowing threads that are free
to grab unstarted tasks from their neighbors’ queues.
- Java Notes-7
- Java Notes-7
- Java - Notes
- notes[java]
- java notes
- java notes
- Java Notes
- Java Notes
- Java 游戏开发Notes
- Java Thread - Notes
- Java 游戏开发Notes
- Java-Notes Documents
- java 访问notes domino
- java 读取notes数据库
- Java Container Learning notes
- Java 游戏开发Notes
- Java Notes: ArrayList
- Notes:java comile flag
- C++输出输入流复制文件
- 281 逃跑的蠕虫
- 2015数据库系统工程师考试大纲
- 一切成功源于积累——20150103 2015年第一个交易日的疯狂 镑美狂跌2600点 美加疯涨1800点
- 如何给Ubuntu 安装Vmware Tools
- Java Notes-7
- 只有兼容Android才能救微软?
- 张江发布:让科技引领时尚
- Factorial Trailing Zeroes (java)
- [leetcode 87] Scramble String
- 购物车的实现方法
- 一切成功源于积累——20150103 太容易被盘面所骗 万恶的侥幸心理
- 【Leetcode】Excel sheet Column title
- Good Bye 2014 A,B,C,D,E