14
Multi-Threaded Application CSNB534 Asma Shakil

Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into

Embed Size (px)

Citation preview

Page 1: Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into

Multi-Threaded Application

CSNB534

Asma Shakil

Page 2: Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into

Overview

Software applications employ a strategy called multi-threaded programming to split tasks into manageable units that can be completed concurrently.

This is an important concept in Java networking as networking clients and servers must often perform several different tasks at a time (i.e. listening for incoming requests and responses, processing data, and updating the text or graphical user interface for the user.

Page 3: Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into

Understand… Single-Threaded Programming

Program statements are executed one after another in a sequential manner. Multiprocess Programming

As, in Unix. Each application runs as a process, with memory allocated for its code and

data. Illusion of multiple processes running.

CPU switches time between the different processes. Disadvantage:

When a process spawns, there is an overlap of data storage between the two processes. Wastage of memory.

IPC is not easy. Multi-Threaded Programming.

Allows a program to have multiple instances of itself running, while using the same shared memory space and code.

The OS maintains a queue of threads and allocates CPU time to them. Issues

Scheduling. Synchronization.

Page 4: Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into

Multi-threading in Java

Java supports multi-threaded applications Threads of execution are represented by the

java.lang.Thread class. Code for tasks that are designed to run in a

separate thread is represented by java.lang.Runnable interface.

Developers should be aware of both.

Page 5: Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into

Creating Multi-threaded Application with the Thread Class

The java.lang.Thread class provides methods to start, suspend, resume and stop a thread

Example ExtendThreadDemo

This example shows the thread being created and the output of each

The threads sleep for five seconds, to simulate the occurrence of meaningful work and then terminate (closing the thread)

Page 6: Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into

Observation

run ( ) method was not invoked when the thread was created.

Only invoked when the thread was started by invoking the start ( ) method.

Thread can be created in advanced and started when needed.

The main method terminates once the threads are started yet the application does not terminate until the two threads have finished their work and leave their run() method.

Page 7: Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into

User Threads and Daemon Threads Java makes a distinction between two types of threads

a user thread (master) a daemon thread (slave)

If the Java runtime determines that the only threads running in an application are daemon threads (i.e., there are no user threads), the Java runtime promptly closes down the application.

On the other hand, if at least one user thread is alive, the Java runtime won't terminate your application.

When the main() method initially receives control from the Java runtime, it executes in the context of a user thread. As long as the main-method thread or any other user thread

remains alive, the application will continue to execute. Daemon threads in action.

Page 8: Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into

Creating Multi-threaded Application with the Runnable Interface

Another way to do multithreading in java is to implement the java.lang.Runnable interface.

Since Java does not allow multiple inheritance, so a class that extends the Java.lang.Thread cannot extend any other class.

The Runnable interface defines a single method, run ( ) that must be implemented.

This interface is used to identify classes capable of running as threads.

Page 9: Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into

Advantages

An object is free to inherit from a different class The same Runnable object can be passed to more

than one thread so that several concurrent threads can be using the same code and same data.

Carefully designed applications can minimize overhead, as creating a new Thread instance requires valuable memory and CPU time.

Example RunnableThreadDemo

Page 10: Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into

Observation

Two threads can be seen printing a message to the console

Only one Runnable object created but two different threads run it.

Page 11: Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into

Controlling Threads

Interrupting a Thread Example: SleepyHead

Use the interrupt ( ) method. In the example

Once the thread is sleeping, it cannot awaken itself. The primary thread waits for the user to hit “enter”, then sends

an interrupt message to the sleeping thread. This message is caught by the catch clause in the sleeping

thread. The secondary thread awakens, displays a message and then

terminates allowing the application to close.

Page 12: Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into

Controlling Threads

Stopping a Thread Example: StopMe

Use the stop ( ) method. Terminate a thread before its task has been completed. This requires the controlling thread to maintain a reference

to the thread it wants to shut down. This method is deprecated in the Java 2 platform

Issues of resource sharing

Page 13: Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into

Controlling Threads

Suspending/Resuming Threads Use the suspend ( ) and resume ( ) methods. Depreceated in Java 2.

Yielding CPU Time to another thread. Use the yield( ) method

Page 14: Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into

Controlling Threads

Waiting Until a Thread is Dead. To determine if a thread has died (run() method has finished)

boolean isAlive() Polling technique is inefficient : CPU time utilization.

Better way : Thread.join() method Overloaded version Thread.join(long milliseconds)

Waits for a thread death or the specified number of milliseconds, whichever comes first.

Example: WaitForDeath Use the join ( ) method.