Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into."— Presentation transcript:

1 Multi-Threaded Application CSNB534 Asma Shakil

2 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.

3 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.

4 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.

5 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)

6 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.

7 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.

8 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.

9 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

10 Observation Two threads can be seen printing a message to the console Only one Runnable object created but two different threads run it.

11 Controlling Threads Interrupting a Thread Example: SleepyHeadSleepyHead 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.

12 Controlling Threads Stopping a Thread Example: StopMeStopMe 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

13 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

14 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: WaitForDeathWaitForDeath Use the join ( ) method.


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

Similar presentations


Ads by Google