# Presentation: 009426

## Threading and ConcurrentProgramming in Java

- Queues
- D.W. Denbo

- Java Threads

- 2006-08-08

## Concurrent Design Patterns

- Today we will cover several tools that are provided by JDK 5.0 that implement many of the important design patterns needed for concurrent applications.  Development is greatly simplified by using these advanced tools and no longer needing to “roll” your own from concurrent programming primitives.

- Java Threads

- 2006-08-08

## Outline

- Blocking Queues
- Callables and Futures
- Executors
- Thread Pools
- Scheduled Execution
- Synchronizers

- Java Threads

- 2006-08-08

## Blocking Queues

- A queue is a data structure with two fundamental operations: to add an element to the tail of the queue (put) and to remove an element from the head (poll), also known as a  FIFO queue.  A blocking queue causes a thread to block when you try to add an element when the queue is full or remove an element when the queue is empty.

- Java Threads

- 2006-08-08

- Blocking queue operations fall into three categories, depending on response.
- add, remove, and element operations throw an exception when you try to add to a full queue or get the head of an empty queue.
- offer and poll methods with a timeout.
- put and take block if the queue is full or empty, respectively

- Java Threads

- 2006-08-08

## Variations on a Blocking Queue

- LinkedBlockingQueue
- Unbound blocking queue implemented using a linked list
- ArrayBLockingQueue
- Fixed capacity blocking queue with fairness option
- PriorityBlockingQueue
- A priority queue not FIFO.
- DelayQueue
- Contains objects that implement the Delayed interface.  Elements can only be removed if their delay has elapsed.

- Java Threads

- 2006-08-08

## BlockingQueueTest

- Java Threads

- 2006-08-08

## Thread-safe Collections

- If multiple threads concurrently modify a data structure such as a hash table, it is easily possible to damage the data structure.  For example, one thread could begin inserting a new element.  If it is preempted while in the middle of routing links and another thread starts traversing the same list, it may follow invalid links and create havoc.

- Java Threads

- 2006-08-08

## Efficient Queues and Hash Tables

- java.util.concurrent supplies efficient implementations for a queue and a hash table, ConcurrentLinkedQueue and ConcurrentHashMap.  By default, it is assumed that there are up to 16 *simultaneous* writer threads.  There can be more than 16 writers, any in excess of 16 are temporarily blocked.  These classes return *weakly consistent* iterators.

- Java Threads

- 2006-08-08

## Older Thread-Safe Collections

- Ever since JDK 1.0, the Vector and Hashtable classes provided thread-safe implementations of a dynamic array and a hash table.  In JDK  1.2, these classes were declared obsolete and replaced by the ArrayList and HashMap classes.  These newer classes are *NOT* thread-safe.

- Java Threads

- 2006-08-08

## Any collection class can be made thread-safe by using a synchronization wrapper.

**List synchAL = Collection.synchronizedList(new ArrayList());**
**Map synchHM = Collection.synchronizedMap(new HashMap());**
**// to iterate over the collection**
**synchronized(synchHM) {**
**	****Iterator ier = synchHM.keySet().iterator();**
**	****while(iter.hasNext()) ... ;**
**}**

- Java Threads

- 2006-08-08

## Callables and Futures

**Callables and Futures**

- A Runnable encapsulates a task that runs asynchronously; like a method with no parameters and no return value.
- A Callable is similar to Runnable, but it returns a value.

**public interface Callable<V> {**
**	****V call() throws Exception;**
**}**

- Java Threads

- 2006-08-08

## A Future holds the result of an asynchronous computation.  You use a Future object so that you can start a computation, give the result to someone, and forget about it.

- A Future holds the *result* of an asynchronous computation.  You use a Future object so that you can start a computation, give the result to someone, and forget about it.

**public interface Future<V> {**
**	****V get() throws ...;**
**	****V get(long timeout, TimeUnit unit) throws ...;**
**	****void cancel(boolean mayInterrupt);**
**	****boolean isCancelled();**
**	****boolean isDone();**
**}**

- Java Threads

- 2006-08-08

## The first call method blocks until the computation is finished.
The second throws a TimeoutException if the call is timed out before the computation is finished.
isDone returns false if the computation is still in progress.
You can cancel the computation with cancel.  If the computation is not yet started it will be cancelled if the mayInterrupt parameter is true, it will be interrupted.

- The second throws a TimeoutException if the call is timed out before the computation is finished.
- isDone returns false if the computation is still in progress.
- You can cancel the computation with cancel.  If the computation is not yet started it will be cancelled if the mayInterrupt parameter is true, it will be interrupted.

**public interface Future<V> {**
**	****V get() throws ...;**
**	****V get(long timeout, TimeUnit unit) throws ...;**
**	****void cancel(boolean mayInterrupt);**
**	****boolean isCancelled();**
**	****boolean isDone();**
**}**

- Java Threads

- 2006-08-08

## FutureTask Wrapper

**FutureTask Wrapper**

- The FutureTask wrapper is a convenient mechanism for turning a Callable into both a Future and a Runnable!  For example,

**Callable<Integer> myComp = ...;**
**FutureTask<Integer> task = new FutureTask<Integer>(myComp);**
**Thread t = new Thread(task);  // it’s a Runnable**
**t.start();**
**...**
**Integer result = task.get(); // it’s a futurer**

- Java Threads

- 2006-08-08

## FutureTest

- Java Threads

- 2006-08-08

## Executors

- Constructing a new thread is somewhat expensive because it involves interaction with the operating system. If you create a large number of short-lived threads, then it should instead use a *thread pool*. A thread pool contains a number of idle threads that are ready to run.  You give a Runnable to the pool, and one of the threads calls the run method.

- Java Threads

- 2006-08-08

## Executors Factory Methods

- newCachedThreadPool	New threads are created as 	needed; idle threads are
- 	kept for 60 seconds
- newFixedThreadPool	The pool contains a fixed set of
- 	threads; idle threads kept
- 	indefinitely.
- newSingleThreadExecutor	A “pool” with a single thread 	that executes tasks sequentially
- newScheduledThreadPool	A fixed-time pool for scheduled
- 	execution
- newSingleThreadScheduledExecutor	A single-thread “pool” for
- 	scheduled execution.

- Java Threads

- 2006-08-08

## Thread Pools

**Thread Pools**

- You can submit a Runnable or Callable to an ExecutorService with one of the following:

**Future<?> submit(Runnable task)**
**Future<T> submit(Runnable task, T result)**
**Future<T> submit(Callable<T> task)**

- The pool will run the submitted task at its earliest convenience.  When you call submit, you get back a Future object that you can use to query the state of the task.

- Java Threads

- 2006-08-08

- Call the static newCachedThreadPool or newFixedThreadPool method of the Executors class.
- Call submit to submit Runnable or Callable objects.
- If you want to be able to cancel a task, hang on to the returned Future objects.
- Call shutdown when you no longer want to submit tasks.

- Java Threads

- 2006-08-08

## Scheduled Execution

- The ScheduledExecutorService interface has methods for scheduled or repeated execution of tasks.
- Executors can also be used to control a group of related tasks.  For example, you can cancel all the tasks in an executor with the shutdownNow method.

- Java Threads

- 2006-08-08

## Synchronizers

- The java.util.concurrent package contains several classes that help manage a set of collaborating threads.  These mechanisms have “canned functionality” for common rendezvous patterns between threads.

- Java Threads

- 2006-08-08

- CyclicBarrier
- Allows a set of threads to wait until a predefined count of them has reached a common barrier, then optionally executes an action.
- Use when a number of threads need to complete before their resuls can be used

- Java Threads

- 2006-08-08

- CountDownLatch
- Allows a set of threads to wait until a count has been decremented to zero.
- Use when one or more threads need to wait until a specified number of results are available.

- Java Threads

- 2006-08-08

- Exchanger
- Allows two threads to exchange objects when both are ready for the exchange.
- Use when two threads work on two instances of the same data structure, one by filling an instance and the other by emptying it.

- Java Threads

- 2006-08-08

- SynchronousQueue
- Allows a thread to hand off an object to another thread.
- Use to send an object from one thread to another when both are ready, without explicit synchronization.

- Java Threads

- 2006-08-08

- Semaphore
- Allows a set of threads to wait until permits are available fro proceeding.
- Used to restrict the total number of threads that can access a resource.  If permit count is one, use to block threads until another thread gives permission.

- Java Threads

- 2006-08-08

## Barriers

**Barriers**

- CyclicBarrier implements a rendezvous called a *barrier*.  Consider a number of threads working on parts of a computation, when all parts are ready, the results need to be combined.

**CyclicBarrier barrier = new CyclicBarrier(nthreads);**
**public void run() {**
**	****doWork();**
**	****barrier.await();**
**	****....**
**}**

- Java Threads

- 2006-08-08

- You can supply an optional *barrier action*.

**Runnable bAction = ...;**
**CyclicBarrier barrier = new CyclicBarrier(nthread, bAction);**

- The action can harvest the result of the individual threads.  The barrier is cyclic because it can be reused after all the waiting threads have been released.

- Java Threads

- 2006-08-08

## Countdown Latches

- A CountDownLatch lets a set of threads wait until a count has reached zero.  It differs from a barrier:
- Not all threads need to wait for the latch until it can be opened.
- The latch can be counted down by external events.
- The countdown latch is one-time only.  It can’t be reused.

- Java Threads

- 2006-08-08

## Semaphores

- Conceptually, a semaphore manages a number of *permits*.  To proceed past the semaphore, a thread requests a permit by calling acquire.  Only a fixed number of permits are available.  Other threads may issue permits by calling release.
- For example, a semaphore with a permit count of 1 is useful as a gate that can be opened and closed by another thread.  Imagine a program that does some work, then waits for a user to study results and press a button to continue.

- Java Threads

- 2006-08-08

## AlgorithmAnimation

- Java Threads

- 2006-08-08

## Next Week

- Lets meet on Wednesday and close out the series with a discussion Threads and Swing.

- Java Threads

- 2006-08-08