100 likes | 227 Views
Java 5 Threading. CSE301 University of Sunderland Harry Erwin, PhD. 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
E N D
Java 5 Threading CSE301 University of Sunderland Harry Erwin, PhD
Introduction • References include: • McLaughlin and Flanagan, 2004, Java 1.5 Tiger: A Developer’s Notebook, O’Reilly • Flanagan, 2005, Java in a Nutshell, 5th edition, O’Reilly • Oaks and Wong, 2004, Java Threads, 3rd edition, O’Reilly
What was added for Java 5? • Some new thread utilities to support pools of threads and thread scheduling. • Explicit locks and condition variables for synchronisation between threads. • Atomic classes to allow developers to avoid explicit synchronisation while writing concurrent applications.
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.
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
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.
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.
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.
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().
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.