Presentation is loading. Please wait.

Presentation is loading. Please wait.

2006-08-02 Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.

Similar presentations


Presentation on theme: "2006-08-02 Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo."— Presentation transcript:

1 2006-08-02 Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo

2 2006-08-02Java Threads2 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. 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 2006-08-02Java Threads3 Outline What are Threads? Advantages Limitations Java concurrency support Interrupting Threads Thread States What are Threads? Advantages Limitations Java concurrency support Interrupting Threads Thread States

4 2006-08-02Java Threads4 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! 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 2006-08-02Java Threads5 Advantages Reactive programming Availability Controllability Active objects Asynchronous messages Parallelism Required concurrency Reactive programming Availability Controllability Active objects Asynchronous messages Parallelism Required concurrency

6 2006-08-02Java Threads6 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 2006-08-02Java Threads7 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 2006-08-02Java Threads8 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.

9 2006-08-02Java Threads9 Active Objects Software objects often model read objects. Most real objects display independent, autonomous behavior.

10 2006-08-02Java Threads10 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.

11 2006-08-02Java Threads11 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 2006-08-02Java Threads12 Required Concurrency Some Java features require threaded applications. For example, audio clips and proper updating of graphics during animations and status information updating.

13 2006-08-02Java Threads13 Limitations Safety Liveness Nondeterminism Threads versus method calls Objects versus activities Thread construction overhead Context-switching overhead Synchronization overhead Threads versus processes Safety Liveness Nondeterminism Threads versus method calls Objects versus activities Thread construction overhead Context-switching overhead Synchronization overhead Threads versus processes

14 2006-08-02Java Threads14 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 2006-08-02Java Threads15 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 2006-08-02Java Threads16 Nondeterminism Multithreaded activities can be arbitrarily interleaved. No two executions of the same program need be identical.

17 2006-08-02Java Threads17 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 2006-08-02Java Threads18 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 2006-08-02Java Threads19 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 2006-08-02Java Threads20 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 2006-08-02Java Threads21 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 2006-08-02Java Threads22 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 2006-08-02Java Threads23 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 Java.lang.Thread Keywords - synchronized and volatile Methods - wait, notify, and notifyAll Blocking Queues Thread-safe collections Callables and Futures Executors Synchronizers

24 2006-08-02Java Threads24 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. 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 2006-08-02Java Threads25 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. 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 2006-08-02Java Threads26 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 } 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 2006-08-02Java Threads27 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. 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 2006-08-02Java Threads28 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) 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 2006-08-02Java Threads29 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. 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


Download ppt "2006-08-02 Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo."

Similar presentations


Ads by Google