Threading and Concurrent Programming in Java Introduction and






























- Slides: 30
Threading and Concurrent Programming in Java Introduction and Definitions D. W. Denbo 2006 -08 -02 Java Threads 1
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. 2006 -08 -02 Java Threads 2
Outline • What are Threads? • Advantages • Limitations • Java concurrency support • Interrupting Threads • Thread States 2006 -08 -02 Java Threads 3
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! 2006 -08 -02 Java Threads 4
Advantages • • Reactive programming Availability Controllability Active objects Asynchronous messages Parallelism Required concurrency 2006 -08 -02 Java Threads 5
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. 2006 -08 -02 Java Threads 6
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 2006 -08 -02 Java Threads 7
Controllability Activities can be suspended, resumed, and stopped by other objects. (NOTE: don’t use the stop, suspend, or resume methods. ) This is done by setting flags or raising exceptions within the thread. 2006 -08 -02 Java Threads 8
Active Objects Software objects often model read objects. Most real objects display independent, autonomous behavior. 2006 -08 -02 Java Threads 9
Asynchronous Messages • When one object sends a message to another, the sender doesn’t always care when the resulting action is performed. Threads allow the first object to continue its own activity without waiting. 2006 -08 -02 Java Threads 10
Parallelism Multiple CPUs 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. 2006 -08 -02 Java Threads 11
Required Concurrency Some Java features require threaded applications. For example, audio clips and proper updating of graphics during animations and status information updating. 2006 -08 -02 Java Threads 12
Limitations • • • Safety Liveness Nondeterminism Threads versus method calls Objects versus activities Thread construction overhead Context-switching overhead Synchronization overhead Threads versus processes 2006 -08 -02 Java Threads 13
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. 2006 -08 -02 Java Threads 14
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. 2006 -08 -02 Java Threads 15
Nondeterminism Multithreaded activities can be arbitrarily interleaved. No two executions of the same program need be identical. 2006 -08 -02 Java Threads 16
Threads versus Method Calls Threads are not very useful for request/replystyle 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 requestexecute-reply sequence. 2006 -08 -02 Java Threads 17
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. 2006 -08 -02 Java Threads 18
Thread Construction Overhead Constructing a thread and setting it in motion is typically slower and more memoryintensive 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. 2006 -08 -02 Java Threads 19
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. 2006 -08 -02 Java Threads 20
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. 2006 -08 -02 Java Threads 21
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. 2006 -08 -02 Java Threads 22
JDK 5. 0 Concurrency Support • • Java. lang. Thread Keywords - synchronized and volatile Methods - wait, notify, and notify. All Blocking Queues Thread-safe collections Callables and Futures Executors Synchronizers 2006 -08 -02 Java Threads 23
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. 2006 -08 -02 Java Threads 24
while (!Thread. current. Thread(). is. Interrupted()) { // do more work } However, if a thread is blocked, it cannot check the interrupted status. This is where the Interrupted. Exception is used. 2006 -08 -02 Java Threads 25
public void run() { try { while(!Thread. current. Thread(). is. Interrupted()) { // do more work } } catch(Interrupted. Exception ie) { // thread was interrupted during sleep or wait }finally { // cleanup, if required } // exiting the run method terminates the thread } 2006 -08 -02 Java Threads 26
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. 2006 -08 -02 Java Threads 27
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) 2006 -08 -02 Java Threads 28
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. 2006 -08 -02 Java Threads 29
2006 -08 -02 Java Threads 30