interactive java note - m06_un012_p02

Upload: ebby-kyle

Post on 06-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    1/36

    Programming Language (JAVA)

    Unit 6.12 - Basics of Threading

    Presentation 2

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    2/36

    Objectives

    At the end of this presentation, you will beable to

    Create multithreaded application

    List the methods involved in life cycle of athread

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    3/36

    Single Threaded Program - Example

    A process that is made of only one thread is called singlethreaded application.

    class Thread_1

    {

    public static void main(String args[])

    {

    }

    }

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    4/36

    Hands-On!

    The program Thread_1.java displays the text,Single Threaded in a single threaded application:

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    5/36

    Creating Threads

    Main Thread

    Switching

    Thread 2Thread 1

    .. .. ..

    .. .. ..

    .. .. ..

    In a single threaded application, main() itself acts as a thread.If a program requires more than one thread, it needs to be created

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    6/36

    Declaring a Thread Class - Example

    class Thread_2 extends Thread

    {

    .. .. ..

    .. ..

    }

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    7/36

    Declaring a Thread class - Syntax

    class extends Thread

    {

    .. .. ..

    .. ..

    }

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    8/36

    Implementing the run() method

    public void run()

    {

    }

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    9/36

    Starting new Thread - Example

    Thread_2 obj = new Thread_2();

    obj.start();

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    10/36

    Starting a new Thread - Syntax

    Threadname objectname = new Threadname;

    objectname.start();

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    11/36

    Hands-On!

    The program ThreadDemo.java illustrates thecreation of multithreaded application

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    12/36

    Activity 6.12.1

    Step 1: Open ThreadDemo.java data file.Step 2: Type class Thread_2 extends Thread in

    code line 3.Step 3: Save the program as ThreadDemo.javaStep 4: Compile the program.

    Step 5: Execute the program.

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    13/36

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    14/36

    Activity 6.12.2

    Step 1: Open ThreadDemo2.java from Student data file.Step 2: Edit the code line 18 as int c = 3, d = 4, e = 5,

    product;.

    Step 3: Edit the code line 19 as product = c * d * e;Step 4: Edit the code line 21 as System.out.println(("Product is

    "+product);

    Step 5: Save the program as ThreadDemo2.java.Step 6: Compile the program.Step 7: Execute the program.

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    15/36

    Stopping and Blocking Threads

    A thread during the running state can be movedto the not runnable state. This can be achieved

    by: Stopping a thread

    Blocking a thread

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    16/36

    Stopping a Thread - Example

    obj.stop();

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    17/36

    Stopping a Thread - Syntax

    objectname.stop();

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    18/36

    Blocking a Thread

    A thread can be temporarily suspended or blocked fromrunning. The thread is blocked from entering therunnable or running state. A thread can be blocked

    using any of the three methods.sleep() - The thread is blocked for a specified time. The

    thread returns to the runnable state when thespecified time is elapsed.

    suspend() - The thread is suspended until furtherordered. The resume() method is called.

    wait() - The thread is blocked until a condition occurs.The notify() method is called.

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    19/36

    Methods in Thread ClassMethods Description

    start() Used to initiate the thread.

    stop() Used to kill the thread.

    run() Used to implement the body of thethread and its behaviour.

    yield()Used to give the control to anotherthread of equal priority.

    suspend() Used to suspend the thread.

    resume() Used to resume the suspendedthread.

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    20/36

    Methods in Thread Class Contd

    sleep(t) Used to specify the time framefor suspension.

    wait() Used to make the thread waituntil some event occurs.

    notify() Used to schedule the thread torun again.

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    21/36

    Life Cycle of a Thread

    New Thread Not runnable

    Dead

    Runnable

    run() method terminates.

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    22/36

    States of Threads

    1. Newborn state

    2. Runnable state

    3. Running state

    4. Blocked state

    5. Dead state.

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    23/36

    States in a Life Cycle of a ThreadStates Description

    Newborn When a thread is created.

    Runnable It is ready for execution and iswaiting for the availability ofCPU.

    Running It is the time given for theexecution of the thread.

    Blocked The thread is suspended,sleeping or waiting to fulfillcertain requirements.

    Dead A thread completes executingthe run() method, ends its life.

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    24/36

    Newborn State

    A thread object when created is said to be innewborn state.

    In this state, the thread can be schedule forrunning state either using start() method or kill itusing stop() method.

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    25/36

    Runnable State

    A thread when ready for execution and is waiting for theavailability of CPU said to be in runnable state.

    The thread in this state joins the queue of threads that arewaiting for execution.

    The threads in the queue are executed based on thegiven priority.

    The thread gives up the control to another thread of equal

    priority using the yield() method.

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    26/36

    Running State

    A thread when given time for its execution issaid to be in running state.

    The thread runs until it loses the control on itsown or a higher priority thread prevents it.

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    27/36

    suspend() method

    The thread is suspended using suspend() method.A suspended thread can be revived using theresume() method.

    This is possible when a thread is required to besuspended for sometime but is not killed.

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    28/36

    sleep() method

    The thread is suspended using sleep(t) method.

    A suspended thread can be revived using the

    resume() method. This is possible when a thread is required to be

    suspended for sometime but is not killed.

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    29/36

    wait() method

    The thread is made to wait until some eventoccurs. This is done using wait()

    The thread is scheduled to run again usingnotify()

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    30/36

    Blocked State

    When the thread is suspended, sleeping orwaiting to fulfil certain requirements, the

    thread is said to be in a blocked state. The blocked thread is neither in a runnable

    state nor in a dead state.

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    31/36

    Dead State

    A thread that has completed the run() method, ends its life.

    The thread is in the dead state. When athread is stopped implementing the stop() method it has reached the dead state.

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    32/36

    Hands-On!

    The ThreadDemo3.java program illustrates thelife cycle of a thread.

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    33/36

    Activity 6.12.3

    Step 1: Open ThreadDemo3.java data file.

    Step 2 : Identify the methods that areinvolved in the life cycle of a thread.

    Step 3 : List the methods involved in the lifecycle of a thread.

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    34/36

    Lab Exercise

    1. Write a program that has two threads. Onethread should calculate the square of a

    number while the other thread shouldcalculate the cube of another number.Display the square and cube of the numbers.

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    35/36

    Summary

    In this presentation, you learnt the following

    A process that is made of only one thread is called

    single threaded application. A single threadedapplication can perform only one task at a time.

    In a single threaded application, main() itself actsas a thread. If a program requires more than onethread, it needs to be created.

    The lifetime of a thread includes the newborn,runnable, running, blocked and dead states.

  • 8/3/2019 Interactive Java Note - M06_UN012_P02

    36/36

    Assignment

    1. Explain the Life cycle of a thread?

    2. Write a program that has two threads.One thread should calculate the area ofthe circle; the second thread shouldcalculate the area of a square. Display theresults. List the methods of the Threadclass in this program.