Sie sind auf Seite 1von 7

Notes

Prepared By Jitendra Chittoda

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

Field Code Chan

Field Code Chan

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

Field Code Chan

Field Code Chan

Field Code Chan

Field Code Chan

Field Code Chan

Field Code Chan

Delayed interface ................................ ................................ ................................ ............... 4 Callable<V> interface................................ ................................ ................................ .......... 4 Executor interface ................................ ................................ ................................ .............. 4 ExecutorService................................ ................................ ................................ .................. 5 AbstractExecutorService abstract class ................................ ................................ ....... 5

Field Code Chan

Field Code Chan

Field Code Chan

Field Code Chan

1.5.1 1.6 1.7 1.8

Field Code Chan

Future<V> interface................................ ................................ ................................ ............ 6 RejectedExecutionHandler interface................................ ................................ ................... 6 CompletionService<V> interface................................ ................................ ....................... 76

Field Code Chan

Field Code Chan

Field Code Chan

1 java .util.concurrent package


1.1 BlockingQueue<E> interface
A Queue that additionally supports operations that wait for the queue to become nonempty when retrieving an element, and wait for space to become available in the queue when storing an element. A BlockingQueue does not accept null elements. Implementations throw NullPointerException on attempts to add, put or offer a null. A null is used as a sentinel value to indicate failure of poll operations. A BlockingQueue may be capacity bounded. At any given time it may have a remainingCapacity beyond which no additional elements can be put without blocking. A BlockingQueue without any intrinsic capacity constraints always reports a remaining capacity of Integer.MAX_VALUE .
E take()

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.

Both the above methods are blocking method calls.

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

Bounded Yes No No Yes (optional)

Order FIFO Priority FIFO Rendezvous Channel

1.2 Delayed interface


This interface exposes a method getDelay(TimeUnit) A mix-in style interface for marking objects that should be acted upon after a given delay. An implementation of this interface must define a compareTo method that provides an ordering consistent with its getDelay method. This interface is used when using DelayQueue class discussed above.

1.3 Callable<V> interface


A task that returns a result and may throw an exception. Implementors define a single method with no arguments called call. The Callable interface is similar to Runnable , in that both are designed for classes whose instances are potentially executed by another thread. A Runnable , however, does not return a result and cannot throw a checked exception.

1.4 Executor interface


An object that executes submitted Runnable tasks. This exposes a method
void execute(Runnable command)

Executes the given command at some time in the future.

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 ()

Returns true if this executor has been shut down.


boolean isTerminated ()

Returns true if all tasks have completed following shut down.


void shutdown()

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

AbstractExecutorService abstract class

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

factory and handler.


ThreadPoolExecutor (int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue <Runnable> workQueue, RejectedExecutionHandler handler)

Creates a new ThreadPoolExecutor with the given initial parameters.


ThreadPoolExecutor (int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue <Runnable> workQueue, ThreadFactory threadFactory)

Creates a new ThreadPoolExecutor with the given initial parameters.


ThreadPoolExecutor (int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue <Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)

Creates a new ThreadPoolExecutor with the given initial parameters.


protected void beforeExecute (Thread t, Runnable r)

Method invoked prior to executing the given Runnable in the given thread.
void execute(Runnable command)

Executes the given task sometime in the future.


<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.
int prestartAllCoreThreads ()

Starts all core threads, causing them to idly wait for work.
boolean prestartCoreThread ()

Starts a core thread, causing it to idly wait for work.

1.6 Future<V> interface


A Future represents the result of an asynchronous computation.
boolean cancel(boolean mayInterruptIfRunning)

Attempts to cancel execution of this task.


V get()

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 ()

Returns true if this task was cancelled before it completed normally.


boolean isDone()

Returns true if this task completed.

1.7 RejectedExecutionHandler interface


A handler for tasks that cannot be executed by a ThreadPoolExecutor .
void rejectedExecution (Runnable r, ThreadPoolExecutor executor)

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

Das könnte Ihnen auch gefallen