Beruflich Dokumente
Kultur Dokumente
Table of Contents
1 java .util.concurrent package ................................ ................................ ................................ ..... 3 1.1 BlockingQueue<E> interface................................ ................................ ............................... 3 ArrayBlockingQueue<E> class ................................ ................................ ..................... 3 DelayQueue class................................ ................................ ................................ ........ 3 LinkedBlockingQueue class ................................ ................................ ......................... 3 PriorityBlockingQueue class ................................ ................................ ........................ 3 SynchronousQueue................................ ................................ ................................ ..... 4 Conclusion ................................ ................................ ................................ .................. 4
1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.2 1.3 1.4 1.5
Delayed interface ................................ ................................ ................................ ............... 4 Callable<V> interface................................ ................................ ................................ .......... 4 Executor interface ................................ ................................ ................................ .............. 4 ExecutorService................................ ................................ ................................ .................. 5 AbstractExecutorService abstract class ................................ ................................ ....... 5
Future<V> interface................................ ................................ ................................ ............ 6 RejectedExecutionHandler interface................................ ................................ ................... 6 CompletionService<V> interface................................ ................................ ....................... 76
Retrieves and removes the head of this queue, waiting if no elements are present on this queue.
void put(E o)
Adds the specified element to this queue, waiting if necessary for space to become available.
1.1.1 ArrayBlockingQueue<E> class A bounded blocking queue backed by an array. 1.1.2 DelayQueue class An unbounded blocking queue of Delayed elements, in which an element can only be taken when its delay has expired. The head of the queue is that Delayed element whose delay expired furthest in the past. If no delay has expired there is no head and poll will return null. Expiration occurs when an element's getDelay(TimeUnit.NANOSECONDS) method returns a value less than or equal to zero. This queue does not permit null elements. 1.1.3 LinkedBlockingQueue class An optionally-bounded blocking queue based on linked nodes. 1.1.4 PriorityBlockingQueue class This queues the objects based on the priorities of the objects those are present in the queue. To define the order of the objects, object should use the Comparable interface or Comparator. Based on the comparison objects are placed in the queue.
If the PriorityBlockingQueue is not defining the Comparable or Comparator then this would result in ClassCastException, because the PriorityBlockingQueue tries to compare two objects using Comparable or Comparator.
1.1.5 SynchronousQueue A blocking queue in which each put must wait for a take, and vice versa. A synchronous queue does not have any internal capacity, not even a capacity of one. You cannot peek at a synchronous queue because an element is only present when you try to take it; you cannot add an element (using any method) unless another thread is trying to remove it; you cannot iterate as there is nothing to iterate. The head of the queue is the element that the first queued thread is trying to add to the queue; if there are no queued threads then no element is being added and the head is null . For purposes of other Collection methods (for example contains ), a Synchronou sQueue acts as an empty collection. This queue does not permit null elements. 1.1.6 Conclusion Queue Implementation ArrayBlockingQueue PriorityBlockingQueue DelayQueue LinkedBlockingQueue SynchronousQueue
1.5 ExecutorService
An Executor that provides methods to manage termination and methods that can produce a Future for tracking progress of one or more asynchronous tasks.
boolean isShutdown ()
Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.
List<Runnable> shutdownNow ()
Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.
<T> Future<T> submit(Callable<T> task)
Submits a value-returning task for execution and returns a Future representing the pending results of the task.
Future<?> submit(Runnable task)
Submits a Runnable task for execution and returns a Future representing that task.
1.5.1
1.5.1.1 ThreadPoolExecutor extends AbstractExecutorService class An ExecutorService that executes each submitted task using one of possibly several pooled threads, normally configured using Executors factory methods.
ThreadPoolExecuto r(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue <Runnable> workQueue) Creates a new ThreadPoolExecutor with the given initial parameters and default thread
Method invoked prior to executing the given Runnable in the given thread.
void execute(Runnable command)
Submits a value-returning task for execution and returns a Future representing the pending results of the task.
int prestartAllCoreThreads ()
Starts all core threads, causing them to idly wait for work.
boolean prestartCoreThread ()
Waits if necessary for the computation to complete, and then retrieves its result.
V get(long timeout, TimeUnit unit)
Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.
boolean isCancelled ()
Method that may be invoked by a ThreadPoolExecutor when execute cannot accept a task.
1.8
CompletionService<V> interface
2 Design Patterns
2.1 Observer Pattern
2.1.1 Observer 2.1.2 Observable
ThreadLocal