Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.

Slides:



Advertisements
Similar presentations
1 Multithreaded Programming in Java. 2 Agenda Introduction Thread Applications Defining Threads Java Threads and States Examples.
Advertisements

Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
1 L49 Multithreading (1). 2 OBJECTIVES  What threads are and why they are useful.  How threads enable you to manage concurrent activities.  The life.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
1 Threads (Part I) Introduction to Threads and Concurrency Overview  Introduction to Threads of Computation  Creating Threads in java an Example  Thread.
1 Lecture 16 Introduction to Multithreading and Concurrency Overview  Introduction to Multithreading of Computation  Where are Threads used?  Why should.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Threads CS Introduction to Operating Systems.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Multithreading in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Threading in Java – a Tutorial QMUL IEEE SB. Why Threading When we need to run two tasks concurrently So multiple parts (>=2) of a program can run simultaneously.
Collage of Information Technology University of Palestine Advanced programming MultiThreading 1.
1 Java Threads Instructor: Mainak Chaudhuri
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
10/17/2015Vimal1 Threads By 10/17/2015Vimal2 Why use threads?? It is a powerful programming tool Computer users take it for granted.
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.
Threads Concurrency in Java. What is mult-tasking? Doing more than one task.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Computer Engineering Rabie A. Ramadan Lecture 8. Agenda 2 Introduction Thread Applications Defining Threads Java Threads and States Priorities Accessing.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
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.
Threads.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
1 Introduction to Threads Computers can perform many tasks concurrently – download a file, print a file, receive , etc. Sequential languages such.
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
Object-oriented Programming in Java. © Aptech Ltd. Introduction to Threads/Session 7 2  Introduction to Threads  Creating Threads  Thread States 
Scis.regis.edu ● CS-434: Object-Oriented Programming Using Java Week 8 Dr. Jesús Borrego Adjunct Faculty Regis University 1.
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.
© 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.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
Internet Computing Module II. Threads – Multithreaded programs, thread Priorities and Thread Synchronization.
THREAD MODEL.
Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
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.
1 Multithreading in Java THETOPPERSWAY.COM. 2 Outline  Multithreading & Thread Introduction  Creating threads  Thread Life Cycle  Threads priorities.
Multithreading / Concurrency
Multithreading Lec 23.
Multi Threading.
Java Multithreading.
Lecture 9 Object Oriented Programming Using Java
Multithreading.
Multithreaded Programming in Java
Multithreading in Java
Multithreading in Java
Exception Handling Visit for more Learning Resources.
Multithreading.
Java Based Techhnology
Multithreading.
Multithreaded Programming
21 Threads.
Java Thread.
Multithreading in java.
Threads and Multithreading
Lecture 19 Threads CSE /6/2019.
CMSC 202 Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Multithreading

Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed different users to run programs at the same time. Two Types of Multitasking ◦Process Based ◦A process is, in essence, a program that is executing. Thus, process-based multitasking is the feature that allows your computer to run two or more programs concurrently ◦Thread Based ◦In a thread-based multitasking environment, the thread is the smallest unit of dispatchable code. This means that a single program can perform two or more tasks simultaneously.

Thread Vs Processes Both threads and processes are methods of parallelizing an application. Processes are independent execution units Processes contain their own state information, use their own address spaces A single process may contain more than one threads All threads within a process share the same state and same memory space They communicate with each other directly, because they share same variables.

Introduction to Thread  A threads is a light weighted process which runs concurrently with other threads.  All threads of program defines a separate path of execution.  A single process may contain more than one threads.

Multithreading Concept Java is a multithreaded programming language which means we can develop multithreaded program using Java. A multithreaded program contains two or more parts that can run concurrently and each part can handle different task at the same time making optimal use of the available resources specially when your computer has multiple CPUs. By definition multitasking is when multiple processes share common processing resources such as a CPU. Multithreading extends the idea of multitasking into applications where you can subdivide specific operations within a single application into individual threads. Each of the threads can run in parallel. The OS divides processing time not only among different applications, but also among each thread within an application. Multithreading enables you to write in a way where multiple activities can proceed concurrently in the same program.

Life Cycle of Multithreading A thread goes through various stages in its life cycle. For example, a thread is born, started, runs, and then dies. Following diagram shows complete life cycle of a thread. Start() New Thread Runnable Not Runnable New Thread Dead stop() stop(), or Run() exists

Multithreading life cycle New: A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread. Runnable: After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task. Not Runnable : A thread enters “Not Runnable” state when one of these four events occurs: ◦Someone invokes its sleep() method ◦Someone invokes its suspend() method ◦The thread uses its wait() method to wait on a condition variable ◦The thread is blocking on I/O. Dead: A thread can die in two ways ◦Either from natural, A thread dies naturally when its run() method exists normally i.e. it will die naturally after the loop and the run() method completes. ◦Or By killed (stoped), we can also kill a thread at any time by calling its stop() method.

How to Create thread There are two ways to create a thread: ◦By extending Thread class ◦By implementing Runnable interface. The Thread Class ◦Thread class provide constructors and methods to create and perform operations on a thread. Thread class extends Object class and implements Runnable interface. ◦Thread() ◦Thread(String name) ◦Thread(Runnable r) ◦Thread(Runnable r,String name) The thread class has the following methods that are used to control a thread ◦public void start(), which prepares a thread to be run ◦public void run(), actually performs the work of the thread. ◦public final void stop(), dies thread when stop() method is invoked.

Simple thread extending Thread public class BytePrinter extends Thread{ public void run(){ for(int b=-128; b<128; b++){ System.out.println(b); } public class ThreadTest { public static void main(String[] args) { System.out.println(“Constructing the thread…”); BytePrinter bp=new BytePrinter(); System.out.prinln(“Starting the thread …”); bp.start(); System.out.println(“Thre thread has been started.”); }

Simple thread implementing runnable public class BytePrinter implements Runnable{ public void run(){ for(int b=-128; b<128; b++){ System.out.println(b); } public class ThreadTest { public static void main(String[] args) { System.out.println(“Constructing the thread…”); BytePrinter bp=new BytePrinter(); Thread t1=new Thread(bp); System.out.prinln(“Starting the thread …”); t1.start(); System.out.println(“Thre thread has been started.”); }

Thread Priority Every Java thread has a priority that helps the operating system determine the order in which threads are scheduled. Java thread priorities are in the range between MIN_PRIORITY (a constant of 1) and MAX_PRIORITY (a constant of 10). By default, every thread is given priority NORM_PRIORITY (a constant of 5). Threads with higher priority are more important to a program and should be allocated processor time before lower-priority threads. However, thread priorities cannot guarantee the order in which threads execute and very much platform dependent. To set a thread’s priority, use the setPriority( ) method, which is a member of Thread. This is its general form: final void setPriority(int level)

Priority Example class TestMultiPriority extends Thread{ public void run(){ System.out.println("running thread name is:"+Thread.currentThread().getName()); System.out.println("running thread priority is:"+Thread.currentThread().getPriority()); } public static void main(String args[]){ TestMultiPriority m1=new TestMultiPriority(); TestMultiPriority m2=new TestMultiPriority(); m1.setPriority(Thread.MIN_PRIORITY); m2.setPriority(Thread.MAX_PRIORITY); m1.start(); m2.start(); } Output: running thread name is:Thread-0 running thread priority is:10 running thread name is:Thread-1 running thread priority is:1

Thread Synchronization When two or more threads need access to a shared resource, they need some way to ensure that the resource will be used by only one thread at a time. The process by which this is achieved is called synchronization If you declare any method as synchronized, it is known as synchronized method. Synchronized method is used to lock an object for any shared resource. When a thread invokes a synchronized method, it automatically acquires the lock for that object and releases it when the thread completes its task.

Synchronization Example import java.io.IOException; class Table extends Thread{ int n; public Table(int n) { this.n=n; } synchronized void printTable(){//synchronized method for(int i=1;i<=5;i++){ System.out.println(n*i); try{ Thread.sleep(400); }catch(Exception e){System.out.println(e);} } } public void run(){ printTable(); }

//Synchronization Contd…… public class TestSynchronization { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Table obj=new Table(5); Thread t1=new Thread(obj); Thread t2=new Thread(obj); t1.start(); t2.start(); }