multi threading by kamalakar dandu

Upload: kamalakar-dandu

Post on 05-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 Multi Threading by Kamalakar Dandu

    1/21

    Multithreading

  • 8/2/2019 Multi Threading by Kamalakar Dandu

    2/21

    Threads

    What are threads?

    Virtual CPU

  • 8/2/2019 Multi Threading by Kamalakar Dandu

    3/21

    Three Parts of a Thread

    Code Data

  • 8/2/2019 Multi Threading by Kamalakar Dandu

    4/21

    Creating a Thread

    Multithreaded programming

    Multiple threads from same Runnable

    instance

    Threads share same data and code

  • 8/2/2019 Multi Threading by Kamalakar Dandu

    5/21

    Starting a Thread

    Using the start() method

    Placing the thread in runnable state

  • 8/2/2019 Multi Threading by Kamalakar Dandu

    6/21

    Basic Control of Threads

    Testing a thread

    isAlive()

    sleep()

    join()

  • 8/2/2019 Multi Threading by Kamalakar Dandu

    7/21

    New

    Runnable

    Running

    Dead

    otherwise

    Blocked

    Blocked in

    ObjectsLock Pool

    Blocked in

    Objects

    Wait Pool

    Scheduler

    notify()

    interrupt()

    run() completes

    sleep() /join()

    synchronizedLock available

    join() or interrupt()

    wait()

    start()

  • 8/2/2019 Multi Threading by Kamalakar Dandu

    8/21

    Which to Use?

    Implementing Runnable

    Better object-oriented design Single inheritance

    Consistency

    Extending Thread

    Simpler code

  • 8/2/2019 Multi Threading by Kamalakar Dandu

    9/21

    The Object Lock Flag

    Every object has a flag which can be thought of as alock flag

    synchronized allows interaction with the lock flag

  • 8/2/2019 Multi Threading by Kamalakar Dandu

    10/21

    Releasing the Lock Flag

    Released when the thread passes the end of the

    synchronized () code block

    Automatically released when a break or exception isthrown by the synchronized() code block

  • 8/2/2019 Multi Threading by Kamalakar Dandu

    11/21

    synchronized -Putting It Together

    Allaccess to delicate data should besynchronized.

    Delicate data protected by synchronizedshould be private.

  • 8/2/2019 Multi Threading by Kamalakar Dandu

    12/21

    Deadlock

    Two threads waiting for a lock from the other

    It is not detected or avoided It can be avoided by

    Deciding on the order to obtain locks

    Adhering to this order throughout

    Releasing locks in reverse order

  • 8/2/2019 Multi Threading by Kamalakar Dandu

    13/21

    Thread Interaction - wait() and notify() Scenario

    Consider yourself and a cab driver as threads

    The problem How to determine when you are at your destination

    The solution

    You notify the cabbie of your destination and relax

    Cabbie drivers and notifies you upon arrival at yourdestination

  • 8/2/2019 Multi Threading by Kamalakar Dandu

    14/21

    Thread Interaction

    wait() and notify()

    The pools

    Wait pool

    Lock pool

  • 8/2/2019 Multi Threading by Kamalakar Dandu

    15/21

    Monitor Model forSynchronization

    Leave shared data in a consistent state

    Ensure programs cannot deadlock

    Do not put threads expecting different notifications

    in the same wait pool

  • 8/2/2019 Multi Threading by Kamalakar Dandu

    16/21

    The suspend()and resume()Methods

    Have been depredated in JDK 1.2 Should be replaced with wait() andnotify()

  • 8/2/2019 Multi Threading by Kamalakar Dandu

    17/21

    The stop() Method

    Releases the lock before it terminates

    Can leave shared data in an inconsistent state

    Should be replaced with wait() andnotify()

  • 8/2/2019 Multi Threading by Kamalakar Dandu

    18/21

    Lab Session

    Write a program for generating odd and even numbers

    between 1 to 20 using two threads. One thread for odd

    numbers and another for even numbers. Create two classes

    extending thread class.

    You can use the below example as a reference.

    Continued

  • 8/2/2019 Multi Threading by Kamalakar Dandu

    19/21

    class NormalThr extends Thread {public void run() {

    for(int i =0;i

  • 8/2/2019 Multi Threading by Kamalakar Dandu

    20/21

    Exercise (contd..)

    13. A simulation of multiple usersaccessing the StudentArray class is to beprogrammed

  • 8/2/2019 Multi Threading by Kamalakar Dandu

    21/21

    Exercise (Contd..)

    14. When the program is accessingthe Students class it has to be ensuredthat the data is not directly accessed by

    two simulated users at the same time.While data is being updated all otherthreads should be blocked (Write lock) and

    while reading data, all threads trying toupdate the thread should be blocked(Read lock)