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

Slides:



Advertisements
Similar presentations
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Advertisements

1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Day 10 Threads. Threads and Processes  Process is seen as two entities Unit of resource allocation (process or task) Unit of dispatch or scheduling (thread.
Multithreading The objectives of this chapter are:
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Threads CSCI 444/544 Operating Systems Fall 2008.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place  (address space + resources) –Thread is a program.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Java Programming: Advanced Topics
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
111 © 2002, Cisco Systems, Inc. All rights reserved.
 2004 Deitel & Associates, Inc. All rights reserved. 1 Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
1 Web Based Programming Section 8 James King 12 August 2003.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Multithreading in JAVA
Java Thread and Memory Model
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
© Wang Bin 2004 Java Threads. © Wang Bin 2004 In this lesson, you will learn to: u Define the concepts of threads and multithreading  Identify the functions.
Multi-Threading in Java
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Multithreading. Multithreaded Programming A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Multithreading The objectives of this chapter are: To understand the purpose of multithreading To describe Java's multithreading mechanism.
Operating Systems Unit 2: – Process Context switch Interrupt Interprocess communication – Thread Thread models Operating Systems.
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
Software Design 13.1 From controller to threads l Threads are lightweight processes (what’s a process?)  Threads are part of a single program, share state.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Threads by Dr. Amin Danial Asham. References Operating System Concepts ABRAHAM SILBERSCHATZ, PETER BAER GALVIN, and GREG GAGNE.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Java Thread Programming
Multithreading The objectives of this chapter are:
Chapter 4 – Thread Concepts
Introduction to threads
Doing Several Things at Once
Multithreading / Concurrency
Multi Threading.
Java Multithreading.
Chapter 4 – Thread Concepts
Multithreaded Programming in Java
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Operating System (013022) Dr. H. Iwidat
Process Management Presented By Aditya Gupta Assistant Professor
Lecture 21 Concurrency Introduction
Multithreading.
Multithreaded Programming
Threads and Concurrency
Threads Chapter 4.
Chapter 4: Threads & Concurrency
Multithreading in java.
NETWORK PROGRAMMING CNET 441
Chapter 4: Threads.
Lecture 19 Threads CSE /6/2019.
Multithreading The objectives of this chapter are:
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Multi-Threaded Application CSNB534 Asma Shakil

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.

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.

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.

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)

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.

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.

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.

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

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

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.

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

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

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.