Threading and Concurrent Programming in Java - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Threading and Concurrent Programming in Java

Description:

Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Overview We will be covering both the tools, classes and interfaces available ... – PowerPoint PPT presentation

Number of Views:72
Avg rating:3.0/5.0
Slides: 31
Provided by: noaa164
Category:

less

Transcript and Presenter's Notes

Title: Threading and Concurrent Programming in Java


1
Threading and ConcurrentProgramming in Java
  • Introduction and Definitions
  • D.W. Denbo

2
Overview
  • We will be covering both the tools, classes and
    interfaces available within JDK 5.0, and the
    concepts necessary to develop robust
    multithreaded applications.
  • Multithreading is necessary to create
    applications, both GUI and client-server, that
    are both responsive to the user and provide a
    high level of control.

3
Outline
  • What are Threads?
  • Advantages
  • Limitations
  • Java concurrency support
  • Interrupting Threads
  • Thread States

4
What are Threads?
  • A thread, short for thread of control, enables
    multitasking within a single program. While
    processes has a complete set of its own
    variables, threads share the same data.
  • Multithreading changed dramatically in JDK 5.0,
    with the addition of a large number of specialty
    classes and interfaces that support threading.
  • Warning multithreading can get very complex!

5
Advantages
  • Reactive programming
  • Availability
  • Controllability
  • Active objects
  • Asynchronous messages
  • Parallelism
  • Required concurrency

6
Reactive Programming
  • Some programs are required to do more than one
    thing at a time. While it is possible to program
    such systems in a single-threaded manner by
    manually interleaving the different activities,
    this is complicated, fragile, and error-prone.
    Reactive programs are easier to design and
    implement using threads.

7
Availability
  • Concurrency allows you to maintain high
    availability of services. For example, you can
    have one object serve as a gateway interface to a
    service, handling each request by constructing a
    new thread to asynchronously perform the
    associated

8
Controllability
  • Activities can be suspended, resumed, and stopped
    by other objects. (NOTE dont use the stop,
    suspend, or resume methods.) This is done by
    setting flags or raising exceptions within the
    thread.

9
Active Objects
  • Software objects often model read objects. Most
    real objects display independent, autonomous
    behavior.

10
Asynchronous Messages
  • When one object sends a message to another, the
    sender doesnt always care when the resulting
    action is performed. Threads allow the first
    object to continue its own activity without
    waiting.

11
Parallelism
  • Multiple CPUs can be can be used to exploit
    available computing power. Even without multiple
    CPUs, interleaving activities in threads avoids
    delays, for example, waiting for remote
    connection and data transfer.

12
Required Concurrency
  • Some Java features require threaded applications.
    For example, audio clips and proper updating of
    graphics during animations and status information
    updating.

13
Limitations
  • Safety
  • Liveness
  • Nondeterminism
  • Threads versus method calls
  • Objects versus activities
  • Thread construction overhead
  • Context-switching overhead
  • Synchronization overhead
  • Threads versus processes

14
Safety
  • When multiple threads are not completely
    independent, each can be involved in sending
    messages to other objects that may also be
    involved in other threads. These objects must
    use synchronization mechanisms to maintain
    consistent state. Using multiple threads
    involving objects designed to work only in
    sequential settings can lead to hard to debug
    inconsistencies.

15
Liveness
  • Activities within concurrent programs may fail to
    be live. One or more activities can simply stop
    for any number of reasons, for example,
    deadlocking, resource limitations, and uncaught
    exceptions.

16
Nondeterminism
  • Multithreaded activities can be arbitrarily
    interleaved. No two executions of the same
    program need be identical.

17
Threads versus Method Calls
  • Threads are not very useful for
    request/reply-style programming. When one object
    must logically wait for a reply from another in
    order to continue, the same thread should be used
    to implement the entrire request-execute-reply
    sequence.

18
Objects versus Activities
  • There are many fewer asynchronously executing
    concurrent activities than objects. It makes
    sense to create a new thread only when an
    invocation actually generates a new asynchronous
    activity, not automatically whenever constructing
    a new object that may or may not engage in
    asynchronous activities.

19
Thread Construction Overhead
  • Constructing a thread and setting it in motion is
    typically slower and more memory- intensive than
    constructing a normal object or invoking a method
    on it. If an activity is short, then it is much
    faster to just invoke it rather than to use
    threads.

20
Context-switching Overhead
  • When there are more active threads than there are
    CPUs, the Java run-time system occasionally
    switches from running one activity to running
    another, which also entails scheduling --
    figuring out witch thread to run next.

21
Synchronization Overhead
  • Java methods employing synchronizations can be
    slower than those that do not provide proper
    concurrency protection. Between thread and
    synchronization overhead, concurrent programs can
    run more slowly than sequential ones.

22
Threads versus Processes
  • Activities that are intrinsically self-contained
    and sufficiently heavy may be simpler to
    encapsulate into standalone programs. Standalone
    programs can be accessed via system-level
    execution facilities ore remote invocation
    mechanisms rather than as multithreaded
    components of a single process.

23
JDK 5.0 Concurrency Support
  • Java.lang.Thread
  • Keywords - synchronized and volatile
  • Methods - wait, notify, and notifyAll
  • Blocking Queues
  • Thread-safe collections
  • Callables and Futures
  • Executors
  • Synchronizers

24
Interrupting Threads
  • A thread terminates when its run method returns.
    In JDK 1.0, there also was a stop method,
    however, that method is now deprecated.
  • There is no longer a way to force a thread to
    terminate. However, the interrupt method can be
    used to request termination of a thread.

25
  • while (!Thread.currentThread().isInterrupted())
  • // do more work
  • However, if a thread is blocked, it cannot check
    the interrupted status. This is where the
    InterruptedException is used.

26
  • public void run()
  • try
  • while(!Thread.currentThread().isInterrupted())
  • // do more work
  • catch(InterruptedException ie)
  • // thread was interrupted during sleep or wait
  • finally
  • // cleanup, if required
  • // exiting the run method terminates the thread

27
Thread States
  • New - When a thread is created with the new
    operator - the thread is not yet running. It is
    in the new state.
  • Runnable - Once the start method has been invoked
    the thread is runnable.

28
Thread States (cont)
  • Blocked - A thread enters the blocked state
  • By calling the sleep method
  • Thread calls an operations that is blocking on
    I/O
  • Thread tries to acquire a lock
  • Thread waits for a condition
  • Suspend method is invoked. (deprecated)

29
Thread States (cont)
  • Dead - a thread is dead when
  • It dies a natural death because the run method
    exits normally.
  • It dies abruptly because of an uncaught exception.

30
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com