Multithreading A thread is the flow of execution, from beginning to end, of a task in a program. With Java, you can launch multiple threads from a program.

Slides:



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

Practical Session 6 Multitasking vs. multithreading Threads Concurrency vs. Parallelism Java Threads Thread confinement Object/Class Immutability.
13/04/2015Client-server Programming1 Block 6: Threads 1 Jin Sa.
Multiplexing/Demux. CPSC Transport Layer 3-2 Multiplexing/demultiplexing application transport network link physical P1 application transport network.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. Multithreading Example from Liang textbook.
Chapter 32 Multi-threading
Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
Java Threads Part II. Lecture Objectives To understand the concepts of multithreading in Java To be able to develop simple multithreaded applications.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
Multithreading Why multi-threads Defining and creating threads An example with two threads Life cycle of a thread An example with user interface Problem.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Cosc 4755 Phone programming: GUI Concepts & Threads.
Programming in Java; Instructor:Alok Mehta Threads1 Programming in Java Threads.
Multithreading Threads Concept
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
1 Lecture 16 Introduction to Multithreading and Concurrency Overview  Introduction to Multithreading of Computation  Where are Threads used?  Why should.
1 Threads (Part II) Threads and Concurrency (Part II) Overview  Review of Previous Lecture.  Controlling Threads and Thread State.  Example 1: Creating.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Multithreading in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Multithreading.
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.
Chapter 15 Multithreading F Threads Concept  Creating Threads by Extending the Thread class  Creating Threads by Implementing the Runnable Interface.
Java Programming, Second Edition Chapter Seventeen Multithreading and Animation.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 19 Multithreading.
1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Threads CSCE 190 – Java Instructor: Joel Gompert Wed, Aug 3, 2004.
1 Web Based Programming Section 8 James King 12 August 2003.
A Guide to Advanced Java Faculty:Nguyen Ngoc Tu. Concurrent programming in Java How to make all things run-able?
Concurrent Programming and Threads Threads Blocking a User Interface.
C20: Threads see also: ThreadedBallWorld, DropTest, Tetris source examples Not covered: advanced stuff like notify/notifyAll.
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.
Scis.regis.edu ● CS-434: Object-Oriented Programming Using Java Week 8 Dr. Jesús Borrego Adjunct Faculty Regis University 1.
Threading and Concurrency COM379T John Murray –
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.
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
Parallel Processing (CS526) Spring 2012(Week 8).  Shared Memory Architecture  Shared Memory Programming & PLs  Java Threads  Preparing the Environment.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Classes - Intermediate
1 Chapter 19 Multithreading. 2 Objectives F To understand the concept of multithreading and apply it to develop animation (§19.2). F To develop thread.
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
CSC CSC 143 Threads. CSC Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Multithreaded applets
Multithreading / Concurrency
Multithreading Lec 23.
Chapter 13: Multithreading
CS244: Advanced Programming Applications
Multi Threading.
PA1 Discussion.
Multithreaded Programming in Java
CNT 4007C Project 2 Good morning, everyone. In this class, we will have a brief look at the project 2. Project 2 is basically the same with project 1.
Principles of Software Development
Multithreading.
COP 4610L: Applications in the Enterprise Spring 2005
CS203 Lecture 15.
Chapter 15 Multithreading
Computer Science 2 06A-Java Multithreading
Representation and Management of Data on the Internet
Multiplexing/Demux.
Lecture 19 Threads CSE /6/2019.
Interfaces,Packages and Threads
CMSC 202 Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Multithreading A thread is the flow of execution, from beginning to end, of a task in a program. With Java, you can launch multiple threads from a program concurrently. These threads can be executed simultaneously in multiprocessor systems. In single processor system, multiple threads share CPU time

Runnable interface When your program executes as an application, Java interpreter starts a thread for the main method. When your program executes as an applet, the web browser starts a thread to run the applet. You can create additional threads to run concurrent task in the program. Each new thread is an object of a class that implements the Runnable interface or extends a class that implements the Runnable interface. You can create threads either by extending the Thread class or by implementing the Runnable interface. Both Thread and Runnable are defined in the java.lang

Creating Threads by Extending Thread class Thread class contains Constructors Many useful method controlling the thread To create and run a thread Define a class that extends Thread Must override the run() method, which tells the system how the thread will be executed when it runs Create an object running on the thread

Figure // custom thread class public class myThread extends Thread { // constructor public myThread() { ….. } // override the run method public void run() { // tell system how to run … } … }// end of class myThread // client class public class client { ….. // some method public void someMethod() { // create a thread myThread th = new myThread(); // start a thread; th.start(); … } … }// end of class myThread

Example Problem: write a program that creates and runs three threads: The first thread prints the letter a one hundred times The second thread prints the letter b one hundred times The third thread prints the integers 1 through 100. Solution: The program has three independent threads. To run them concurrently, it needs to create a runnable object for each thread The first two thread have similarity, we can define in one thread class and create two different thread for letter a and b.

Program code // TestThread.java public class TestThread{ public static void main(String [] args) { // create threads PrintChar printA = new PrintChar(‘a’,100); PrintChar printB = new PrintChar(‘b’, 100); PrintNum print100 = new PrintNum (100); // start threads printA.start(); printB.start(); print100.start(); } // end of main }// end of class TestThread

Program code - continued class PrintChar extends Thread{ private char charToPrint; private int times; // construcotr public PrintChar(char x, int n){ charToPrint = x; times = n; } // override run() method public void run() { for ( int I = 0; I < times; I++) System.out.print (charToPrint); }

Program code - continued // the thread class for printing number from 1-n class PrintNum extends Thread { private int lastNum; // constructor public PrintNum( int n) { lastNum = n; } // override run() method public void run(){ for ( int I = 0; I < ladtNum; I++) System.out.print( I + “ “); } // end of run } // end of class

Implementing Runnable Interface Java have no multiple inheritance Java provides Runnable interface as alternative to the Thread class Runnable interface just contain the run() method You need to implement this method to tell system how your thread is going to run

// custom thread class public class myThread implements Runnable { // constructor public myThread() { ….. } // implement the run method public void run() { // tell system how to run … } … }// end of class myThread // client class public class client { ….. // some method public void someMethod() { // create an instance of myThread myThread myth = new myThread(…); // create a thread Thread th = new Thread(myth); // start a thread; th.start(); … } … }// end of class myThread

Program Code public class testRunnable{ // create threads Thread printA = new Thread(new PrintChar(‘a’,100)); Thread printB = new Thread(new PrintChar(‘b’,100)); Thread print100 = new Thread (new PrintNum(100)); public static void main(String []args){ new TestRunnable(); }

Program code - continued public TestRunnable(){ printA.start(); printB.start(); print100.start(); } // thread class for printing letter a or b class PrintChar implements Runnable{ private char charToPrint; private int times; public PrintChar(char c, int t){ charToPrint = c; times = t; }

Program code - continued // override the run method public void run(){ for ( int i = 0; i <times; i++) System.out.print(charToPrint); } }// end of class

Program code -continued // class for printing number from 1 to n Class PrintNum implements Runnable{ private int lastNum; public PrintNum(int n){ lastNum = n; } // override run() Public void run(){ for ( int I = 0; I < lastNum; i++){ System.out.print(“ “ + i); }