Java 5 Threading - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Java 5 Threading

Description:

CSE301. University of Sunderland. Harry Erwin, PhD. Introduction ... Oaks and Wong, 2004, Java Threads, 3rd edition, O'Reilly. What was added for Java 5? ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 11
Provided by: harry66
Category:
Tags: harry | java | threading | wong

less

Transcript and Presenter's Notes

Title: Java 5 Threading


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

2
Introduction
  • References include
  • McLaughlin and Flanagan, 2004, Java 1.5 Tiger A
    Developers Notebook, OReilly
  • Flanagan, 2005, Java in a Nutshell, 5th edition,
    OReilly
  • Oaks and Wong, 2004, Java Threads, 3rd edition,
    OReilly

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.4s 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
    indivisibleguaranteed 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.
Write a Comment
User Comments (0)
About PowerShow.com