-one such related pair of patterns is the concept
Of an executor service this manages tasks and that of a thread pool the services tasks in
An efficient.
-the new callable interface, which is effectively
A replacement for Runnable, rectifies this situation by providing a call () method
Both returns a result and can throw exceptions.
-the new Future class was 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 are intended to being an asynchronous task handler. Instead
of an execute () method, it had submit () methods that accept a callable (or runna
ble ) and return immediately with a future object, can be used to manage the task
and collect the result later
-newfixedthreadpool (int)
The 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 would be created to replace it. Threads is never removed from the
Pool until the service is shut down.
Newcachedthreadpool ()
This pool uses a open-ended number of threads that grows and shrinks with de‐
Mand. The main advantage of this service was that threads was cached for a period of
Time and reused, eliminating the overhead of creating new threads for short-lived
Tasks. Threads that is not used for one minute is removed. Tasks are submitted
directly to Threads; There is no real queuing.
Newsinglethreadexecutor ()
This executorservice uses a single thread to execute the tasks from a unbounded
Queue. In this sense, it's 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 is 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 it
For a structure your tasks so, they can be split up as needed to keep all of the
Available threads busy working on the data with as much continuity as possible.
-the Framework Then
Implements what's known as a "work stealing" algorithm, allowing threads
To grab unstarted tasks from their neighbors ' queues.
Java Notes-7