CSCI213 Spring2013 Lectures Multithreading

download CSCI213 Spring2013 Lectures Multithreading

of 16

Transcript of CSCI213 Spring2013 Lectures Multithreading

  • 7/30/2019 CSCI213 Spring2013 Lectures Multithreading

    1/16

    1

    Multithreading

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Topic Goals

    To understand how multiple threadscan execute in parallel

    To learn how to implement threadsTo understand race conditions and

    deadlocks

    To be able to avoid corruption ofshared objects by using locks andconditions

    2

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Process

    Resources ownership process includes a virtual address space to

    hold the process image

    Scheduling/execution follows an execution path that may be

    interleaved with other processes

    These two characteristics are treatedindependently by the operating system

    3

  • 7/30/2019 CSCI213 Spring2013 Lectures Multithreading

    2/16

    2

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Individual execution unit within a process Each thread has a control block, with a

    state (Ready/Running/Blocked), savedregisters, instruction pointer

    Separate stack

    Threads

    4

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Benefits of Threads

    Takes less time to create a new threadthan a process

    Less time to terminate a thread than aprocess

    Less time to switch between two threadswithin the same process

    Since threads within the same processshare memory and files, they cancommunicate with each other withoutinvoking the kernel

    5

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Types

    6

  • 7/30/2019 CSCI213 Spring2013 Lectures Multithreading

    3/16

    3

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Foreground/Background Backing up in background

    Asynchronous Processing Faster Execution

    Read one set of data while processing anotherset

    Organization For a word processing program, may allow

    one thread for each file being edited

    7

    Examples

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Generally, a thread can block withoutblocking the remaining threads in theprocess

    Allow the process to start two operations atonce, each thread blocks on the appropriateevent

    Must handle synchronization betweenthreads System calls or local subroutines Thread generally responsible for getting/

    releasing locks, etc

    Block and Synchronization

    8

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Threads Properties

    Suspending a process involves suspendingall threads of the process since all threadsshare the same address space

    Termination of a process, terminates allthreads within the process

    9

  • 7/30/2019 CSCI213 Spring2013 Lectures Multithreading

    4/16

    4

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Java Threads

    A thread is a program unit that isexecuted independently of other parts ofthe program

    The Java Virtual Machine executes eachthread in the program for a short amountof time

    This gives the impression of parallelexecution

    10

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Using a Thread

    Develop a class that implements the Runnable interfacepublic interface Runnable{

    void run();}

    Place the code for your task into the run method of yourclasspublic class MyRunnable implements Runnable{

    public void run(){// Task statements go here...

    }

    }

    11

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Running a Thread (2)

    Create an object of your subclassRunnable r = new MyRunnable();

    Construct a Thread object from therunnable objectThread t = new Thread(r);

    Call the start method to start thethreadt.start();

    12

  • 7/30/2019 CSCI213 Spring2013 Lectures Multithreading

    5/16

    5

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Example

    A program to print a time stamp andHello World once a second for tenseconds:Fri Apr 12 00:51:09 GST 2013 Hello, World!

    Fri Apr 12 00:51:10 GST 2013 Hello, World!

    Fri Apr 12 00:51:11 GST 2013 Hello, World!

    Fri Apr 12 00:51:12 GST 2013 Hello, World!

    Fri Apr 12 00:51:13 GST 2013 Hello, World!

    Fri Apr 12 00:51:14 GST 2013 Hello, World!

    Fri Apr 12 00:51:15 GST 2013 Hello, World!

    Fri Apr 12 00:51:16 GST 2013 Hello, World!

    Fri Apr 12 00:51:17 GST 2013 Hello, World!

    13

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    public class GreetingRunnable implements Runnable

    {

    public GreetingRunnable(String aGreeting)

    {

    greeting = aGreeting;

    }

    public void run()

    {

    // Task statements go here

    ...

    }

    // Fields used by the task statements

    private String greeting;

    }

    GreetingRunnable

    14

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    rea ct ons orGreetingRunnable

    Print a time stamp Print the greeting Wait a second Loop

    15

  • 7/30/2019 CSCI213 Spring2013 Lectures Multithreading

    6/16

    6

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    GreetingRunnable

    We can get the date and time byconstructing a Date object

    Date now = new Date();

    To wait a second, use the sleep method ofthe Thread class

    sleep(milliseconds)

    A sleeping thread can generate anInterruptedException

    Catch the exception Terminate the thread

    16

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Generic run method

    public void run(){

    try{

    // Task statements}

    catch (InterruptedException exception){}// Clean up, if necessary

    }17

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    01:import java.util.Date;

    02:

    03:/**

    04: A runnable that repeatedly prints a greeting.

    05:*/

    06:publicclass GreetingRunnable implements Runnable

    07: {

    08: /**

    09: Constructs the runnable object.

    10: @param aGreeting: the greeting to display

    11: */

    12: public GreetingRunnable(String aGreeting)

    13: {

    14: greeting = aGreeting;

    15: }

    16:

    17: publicvoid run()

    18: {

    19: try

    20: {

    21: for (int i = 1; i

  • 7/30/2019 CSCI213 Spring2013 Lectures Multithreading

    7/16

    7

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    23: Date now = new Date();24: System.out.println(now + " " + greeting);

    25: Thread.sleep(DELAY);

    26: }

    27: }

    28: catch (InterruptedException exception)

    29: {

    30: }

    31: }

    32:

    33: private String greeting;

    34:

    35: staticfinalint REPETITIONS = 10;

    36: staticfinalint DELAY = 1000;

    37: }

    GreetingRunnable.java (2)

    19

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    To Start the Thread

    Construct an object of your runnable classRunnable t = new GreetingRunnable("Hello

    World");

    Then construct a thread and call the startmethod

    Thread t = new Thread(r);

    t.start();

    20

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    01:/**

    02: This program runs two greeting threads in parallel.

    03:*/

    04:publicclass GreetingThreadRunner

    05: {

    06: publicstaticvoid main(String[] args)

    07: {

    08: GreetingRunnable r1 = new GreetingRunnable( "Hello, World!");

    09: GreetingRunnable r2 = new GreetingRunnable( "Goodbye, World!");

    10: Thread t1 = new Thread(r1);

    11: Thread t2 = new Thread(r2);

    12: t1.start();

    13: t2.start();

    14: }

    15: }

    GreetingThreadRunner.java

    21

  • 7/30/2019 CSCI213 Spring2013 Lectures Multithreading

    8/16

    8

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Fri Apr 12 00:57:46 GST 2013 Hello, World!

    Fri Apr 12 00:57:46 GST 2013 Goodbye, World!

    Fri Apr 12 00:57:47 GST 2013 Hello, World!

    Fri Apr 12 00:57:47 GST 2013 Goodbye, World!

    Fri Apr 12 00:57:48 GST 2013 Goodbye, World!

    Fri Apr 12 00:57:48 GST 2013 Hello, World!

    Fri Apr 12 00:57:49 GST 2013 Hello, World!

    Fri Apr 12 00:57:49 GST 2013 Goodbye, World!

    Fri Apr 12 00:57:50 GST 2013 Hello, World!

    Fri Apr 12 00:57:50 GST 2013 Goodbye, World!

    Fri Apr 12 00:57:51 GST 2013 Hello, World!

    Fri Apr 12 00:57:51 GST 2013 Goodbye, World!

    Fri Apr 12 00:57:52 GST 2013 Goodbye, World!

    Fri Apr 12 00:57:52 GST 2013 Hello, World!

    Fri Apr 12 00:57:53 GST 2013 Hello, World!

    Fri Apr 12 00:57:53 GST 2013 Goodbye, World!

    Fri Apr 12 00:57:54 GST 2013 Goodbye, World!

    Fri Apr 12 00:57:54 GST 2013 Hello, World!

    Fri Apr 12 00:57:55 GST 2013 Hello, World!

    Fri Apr 12 00:57:55 GST 2013 Goodbye, World!

    Output

    22

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Thread Scheduler

    The thread scheduler runs each thread fora short amount of time (a time slice)

    Then the scheduler activates anotherthread

    There will always be slight variations inrunning times especially when calling

    operating system services (e.g. input andoutput)

    There is no guarantee about theorder in which threads are executed

    23

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Terminating ThreadsA thread terminates when its run method

    terminates

    Do not terminate a thread using thedeprecated stop method

    Instead, notify a thread that it shouldterminate

    t.interrupt();

    interrupt does not cause the thread toterminate

    it sets a boolean field in the thread datastructure

    24

  • 7/30/2019 CSCI213 Spring2013 Lectures Multithreading

    9/16

    9

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Terminating Threads (2)

    The run method should check occasionally whether it hasbeen interrupted

    Use the interrupted method An interrupted thread should release resources, clean up, and exit

    public void run(){

    for (int i = 1;i

  • 7/30/2019 CSCI213 Spring2013 Lectures Multithreading

    10/16

    10

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Race Conditions

    When threads share a common object, they can conflictwith each other

    Sample program: multiple threads manipulate a bankaccount

    Here is the run method of DepositRunnable:public void run(){

    try{

    for (int i = 1; i

  • 7/30/2019 CSCI213 Spring2013 Lectures Multithreading

    11/16

    11

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Sample Application (2)

    The result should be zero, but sometimes it is notNormally, the program output looks somewhat like this:Depositing 100.0, new balance is 100.0

    Withdrawing 100.0, new balance is 0.0

    Depositing 100.0, new balance is 100.0

    Depositing 100.0, new balance is 200.0

    Withdrawing 100.0, new balance is 100.0

    ...

    Withdrawing 100.0, new balance is 0.0

    But sometimes you may notice messed-up output, likethis:

    Depositing 100.0Withdrawing 100.0, new balance is

    100.0, new balance is -100.0

    31

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    cenar o to xp a n on-zeroResult: Race Condition

    1.A deposit thread executes the linesSystem.out.print("Depositing " + amount);

    double newBalance = balance + amount;

    The balance field is still 0, and the newBalance localvariable is 100

    2.The deposit thread reaches the end of its time slice anda withdraw thread gains control

    3.The withdraw thread calls the withdraw method whichwithdraws $100 from the balance variable;it is now -100

    4.The withdraw thread goes to sleep32

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    cenar o to xp a n on-zeroResult: Race Condition (2)

    The deposit thread regains control andpicks up where it left off; it executes:

    System.out.println(", new balance is " +

    newBalance);

    balance = newBalance;

    The balance is now 100 instead of 0because the deposit method used the OLDnewBalance

    33

  • 7/30/2019 CSCI213 Spring2013 Lectures Multithreading

    12/16

    12

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Corruptingthe Contentsof thebalance Field

    34

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Race ConditionOccurs if the effect of multiple threads on shared data

    depends on the order in which they are scheduled

    It is possible for a thread to reach the end of its time slicein the middle of a statement

    It may evaluate the right-hand side of an equation butnot be able to store the result until its next turn

    public void deposit(double amount)

    {

    balance =balance + amount;System.out.print("Depositing " + amount + ", new

    balance is " + balance); }

    Race condition can still occur:balance = the right-hand-side value

    35

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Synchronizing Object Access

    To solve problems such as the one justseen, use a lock objectA lock object is used to control threads

    that manipulate shared resources

    In Java: Lock interface and several classesthat implement it

    ReentrantLock: most commonly used lockclass

    36

  • 7/30/2019 CSCI213 Spring2013 Lectures Multithreading

    13/16

    13

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Synchronizing Object Access (2)

    Typically, a lock object is added to a class whosemethods access shared resources, like this:

    public class BankAccount

    {

    public BankAccount()

    {

    balanceChangeLock = new ReentrantLock();

    ...

    }

    ...

    private Lock balanceChangeLock;

    }

    37

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Synchronizing Object Access (3)

    Code that manipulates sharedresource is surrounded by calls tolock and unlock:

    balanceChangeLock.lock();

    Code that manipulates the shared

    resource

    balanceChangeLock.unlock();

    38

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Synchronizing Object Access (4)

    If code between calls to lock and unlock throws anexception, call to unlock never happens

    To overcome this problem, place call to unlock into afinally clause:

    public void deposit(double amount)

    {

    balanceChangeLock.lock();

    try

    {

    System.out.print("Depositing " + amount);

    double newBalance = balance + amount;

    System.out.println(", new balance is " +

    newBalance); balance = newBalance;

    }

    39

  • 7/30/2019 CSCI213 Spring2013 Lectures Multithreading

    14/16

    14

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Synchronizing Object Access (5)finally

    {

    balanceChangeLock.unlock();

    }

    }

    40

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Synchronizing Object Access (6)

    When a thread calls lock, it owns the lockuntil it calls unlock

    A thread that calls lock while anotherthread owns the lock is temporarilydeactivated

    Thread scheduler periodically reactivatesthread so it can try to acquire the lockEventually, waiting thread can acquire thelock

    41

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Avoiding Deadlocks

    A deadlock occurs if no thread can proceed because eachthread is waiting for another to do some work first

    BankAccount examplepublic void withdraw(double amount)

    {

    balanceChangeLock.lock();

    try

    {

    while (balance < amount)

    Wait for the balance to grow

    ...

    }

    finally

    {

    balanceChangeLock.unlock();

    }

    }

    42

  • 7/30/2019 CSCI213 Spring2013 Lectures Multithreading

    15/16

    15

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Avoiding Deadlocks (2)

    How can we wait for the balance to grow?We cant simply call sleep inside withdraw method

    thread will block all other threads that want to usebalanceChangeLock (balance)

    In particular, no other thread can successfully executedeposit

    Other threads will call deposit, but will be blocked untilwithdraw exits

    But withdraw doesnt exit until it has funds availableDEADLOCK

    43

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Condition Objects

    To overcome problem, use a conditionobject

    Condition objects allow a thread totemporarily release a lock, and to regainthe lock at a later time

    Each condition object belongs to a specificlock object

    44

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Condition Objects (2)

    You obtain a condition object with newCondition methodpublic class BankAccount{

    public BankAccount()

    {

    balanceChangeLock = new ReentrantLock();

    sufficientFundsCondition =

    balanceChangeLock.newCondition();

    ...

    }

    ...

    private Lock balanceChangeLock;

    private Condition sufficientFundsCondition;

    }

    45

  • 7/30/2019 CSCI213 Spring2013 Lectures Multithreading

    16/16

    16

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Condition Objects (3)

    It is customary to give the condition objecta name that describes condition to test

    You need to implement an appropriatetest

    46

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Condition Objects (4)

    As long as test is not fulfilled, call await on thecondition object:

    public void withdraw(double amount)

    {

    balanceChangeLock.lock();

    try

    {

    while (balance < amount)

    sufficientFundsCondition.await();

    ...

    }

    finally

    {

    balanceChangeLock.unlock();

    }

    }

    47

    CSEUOWD

    CSCI 213: MultithreadingSpring 2013

    Condition Objects (5)

    Calling await Makes current thread wait Allows another thread to acquire the lock object

    To unblock, another thread must execute signalAll onthe same condition object

    sufficientFundsCondition.signalAll(); signalAll unblocks all threads waiting on the condition signal: randomly picks just one thread waiting on the

    object and unblocks it

    signal can be more efficient, but you need to know thatevery waiting thread can proceed

    Recommendation: always call signalAll48