Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java 5 Threading CSE301 University of Sunderland Harry Erwin, PhD.

Similar presentations


Presentation on theme: "Java 5 Threading CSE301 University of Sunderland Harry Erwin, PhD."— Presentation transcript:

1 Java 5 Threading CSE301 University of Sunderland Harry Erwin, PhD

2 Introduction References include: –McLaughlin and Flanagan, 2004, Java 1.5 Tiger: A Developer’s Notebook, O’Reilly –Flanagan, 2005, Java in a Nutshell, 5 th edition, O’Reilly –Oaks and Wong, 2004, Java Threads, 3 rd edition, O’Reilly

3 What was added for Java 5? 1.Some new thread utilities to support pools of threads and thread scheduling. 2.Explicit locks and condition variables for synchronisation between threads. 3.Atomic classes to allow developers to avoid explicit synchronisation while writing concurrent applications.

4 Concurrency Utilities In the past, if a thread terminated by throwing an exception, the exception went to an internal class called a ThreadGroup, which usually did something inappropriate. To avoid this, you had to extend ThreadGroup, which was a lot of arcane coding. Now you can define an exception handler for the Thread to handle the problem.

5 Thread-safe Collections Java 1.4’s collection classes are generally not thread-safe. If you want to a collection class with threads, you had to use Hashtable or Vector, both of which are old. Java 5 has thread-safe collection classes in java.util.concurrent.*. These include: –ConcurrentHashMap –CopyOnWriteArrayList –CopyOnWriteArraySet –ConcurrentLinkedQueue

6 BlockingQueues Java 5 provides queues for use as first-in-first-out data structures. If these run out of space, you have problems, but you can use java.util.concurrent.BlockingQueue to get around the problem. There are a number of specialised BlockingQueues that you can use. If a BlockingQueue is full, the put() method will wait for room. If it is empty, a take() will wait for data.

7 Separating Thread Logic from Executor Logic Instead of passing a Runnable object to a Thread and scheduling things in detail, you can pass it to an Executor, and have the Executor handle the threading logic. This can also be used to handle call-backs. The Callable interface indicates that the object can be called. It consists of a call() method. You can pass a Callable object to a FutureTask, give the task to a Thread, start the thread, automatically wait, and get the results some time later using a get call on the FutureTask.

8 Advanced Synchronising You have four specialised synchroniser classes: –Semaphore –CountDownLatch –Exchanger –CyclicBarrier These allow you to complex synchronisation such as is done in operating systems running on multiple CPUs.

9 Atomic Types An atomic operation is one that is indivisible— guaranteed to complete without being interrupted by another thread. Security operations need to be atomic, for example. In java.util.concurrent.atomic.*, there are a number of atomic types that allow you to perform atomic operations on primitive types. These include get(), set(), getAndSet(), and compareAndSet().

10 Lock and Condition Extends the synchronized keyword. This allows you to lock variables and wait on those variables becoming unlocked. Other locks are supported including ReadWriteLock.


Download ppt "Java 5 Threading CSE301 University of Sunderland Harry Erwin, PhD."

Similar presentations


Ads by Google