multithreading - copy

19
Multithreading introduction Multithreading introduction Creating threads Creating threads Process and Threads Process and Threads Thread life cycle Thread life cycle Thread priorities Thread priorities Synchronizing threads Synchronizing threads

Upload: gl-saini

Post on 20-Nov-2015

212 views

Category:

Documents


0 download

DESCRIPTION

PPT

TRANSCRIPT

  • Multithreading introduction

    Creating threads

    Process and Threads

    Thread life cycle

    Thread priorities

    Synchronizing threads

  • *MultitaskingMultitasking: refers to a computer's ability to perform multiple jobs concurrentlyMultitasking is divided into two types:

    Process-based: Here two or more programs runs concurrently. You can run Windows calculator and a Text editor (Notepad) at the same time.

    Thread-based: A single program can perform two or more tasks simultaneously. For example, text editor can print while formatting is being done.more than one program are running concurrently

  • What is Thread?

  • A thread is a single sequential flow of control within a program.Thread does not have its own address space but uses the memory and other resources of the process in which it executes. There may be several threads in one processThe Java Virtual Machine (JVM) manages these and schedules them for execution.

  • What is Multithreading ?

  • A thread is a single sequence of execution within a program

    refers to multiple threads of control within a single program

    each program can run multiple threads of control within it, e.g., Web Browser

  • Multithreading enables programmers to do multiple things at one time.

    For ex, we can send tasks such as printing into the background and continue to perform some other task in the foreground.

  • *Concurrency vs. ParallelismCPUCPU1CPU2

  • *Threads and ProcessesCPUProcess 1Process 3Process 2Process 4mainrunGC

  • Processes & Threads

  • 4: Threads*Single and Multithreaded ProcessesTHREADS

    4: Threads

  • Why Multithreading?When multiple events/actions need to occur at the same timeExamples: Download 10 pages. Single-threaded program: sequentially Multithreaded: all at the same time save timeDownload data from the network and respond to mouse at the same time

  • A Single Threaded Programclass ABC{ . ..... . ..... . .....}BeginningSingle-threaded Body of ExecutionEnd

  • A Multithreaded Programstart

    start

    startswitchingswitchingMain Method Module

  • Creating Threads Threads are implemented as objects that contains a method called run() class MyThread extends Thread { public void run() { // thread body of execution } }Create a thread: MyThread thr1 = new MyThread();

    Start Execution of threads: thr1.start();

    Create and Execute: new MyThread().start();

    :: Extending the thread classEXAMPLEThreadEx1 .java

  • *Creating Threads :: Extending the thread class : exampleclass MyThread extends Thread { // the thread public void run() { System.out.println(" this thread is running ... "); }} // end class MyThread

    class ThreadEx1 { // a program that utilizes the thread public static void main(String [] args ) {MyThread t = new MyThread();// due to extending the Thread class (above)// I can call start(), and this will call// run(). start() is a method in class Thread. t.start(); } // end main()} // end class ThreadEx1

  • *Creating Threads class MyThread implements Runnable{ ..... public void run() { // thread body of execution }}Creating Object: MyThread myObject = new MyThread();

    Creating Thread Object: Thread thr1 = new Thread( myObject );

    Start Execution: thr1.start();

    :: Implementing the runnable interfaceEXAMPLEThreadEx2.java

  • Creating Threads :: Implementing the runnable interface : exampleclass MyThread implements Runnable { public void run() { System.out.println(" this thread is running ... "); }} // end class MyThread

    class ThreadEx2 { public static void main(String [] args ) { MyThread myObject = new MyThread(); Thread thr1 = new Thread( myObject ); thr1.start();

    } // end main()} // end class ThreadEx2

  • MultithreadedEXAMPLEThreadTest.java

    * ****