Multithreading - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Multithreading

Description:

Steps to Programming/Running a Threads. Implement a class that extends the Thread class ... Corrupting Data with Unsynchronized Threads ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 30
Provided by: csU82
Category:

less

Transcript and Presenter's Notes

Title: Multithreading


1
  • Multithreading
  • (cont.)

2
Steps to Programming/Running a Threads
  • Implement a class that extends the Thread class
  • Place the code for your task into the run method
    of your class
  • Create an object of your subclass
  • Call the start method of your class to start the
    thread
  • When a Thread object is started, the code in its
    run method is executed in a new thread

3
GreetingThread Outline
  • A program to print a time stamp and "Hello World"
    once a second for ten seconds
  • public class GreetingThread extends Thread
  • public void run()
  • //thread action
  • . . .
  • //variables used by the thread action
  • . . .

4
Thread Action for GreetingThread
  • Print a time stamp
  • Print the greeting
  • Wait a second

5
GreetingThread
  • We can get the date and time by constructing Date
    object Date now new Date()
  • To wait a second, use the sleep method of the
    Thread class sleep(milliseconds)
  • A sleeping thread can generate an
    InterruptedException
  • Catch the exception
  • Terminate the thread

6
GreetingThread run method
  • public void run()
  • try
  • //thread action
  • catch (InterruptedException e)
  • //cleanup, if necessary

7
File GreetingThread.java
  • import java.util.Date
  • /
  • A thread that repeatedly prints a greeting.
  • /
  • public class GreetingThread extends Thread
  • /
  • Constructs the thread object.
  • _at_param aGreeting the greeting to display
  • /
  • public GreetingThread(String aGreeting)
  • greeting aGreeting

8
  • public void run( )
  • try
  • for (int i 1 i lt REPETITIONS i)
  • Date now new Date()
  • System.out.println(now " "
    greeting)
  • sleep(DELAY)
  • catch (InterruptedException exception)
  • private String greeting
  • private static final int REPETITIONS 10
  • private static final int DELAY 1000
  • / end class /

9
To Start the Thread
  • Construct an object of your thread
    class GreetingThread t new
  • GreetingThread("Hello World")
  • Call the start method t.start()

10
File GreetingThreadTest.java
  • import java.util.Date
  • /
  • This program tests the greeting thread by
    running
  • two threads in parallel.
  • /
  • public class GreetingThreadTest
  • public static void main(String args)
  • GreetingThread t1
  • new GreetingThread("Hello, World!")
  • GreetingThread t2
  • new GreetingThread("Goodbye, World!")
  • t1.start()
  • t2.start()

11
Thread Scheduler
  • The thread scheduler runs each thread for a short
    amount of time called a time slice (quantum)
  • Then the scheduler picks another thread from
    those that are runnable
  • A thread is runnable if it is not asleep or
    blocked in some way
  • There is no guarantee about the order in which
    threads are executed

12
Terminating Threads
  • A thread terminates when its run method returns
  • Do not terminate a thread using the deprecated
    stop method
  • Instead, notify a thread that it should terminate
  • t.interrupt()

13
Terminating Threads
  • A thread's run method should check occasionally
    whether it has been interrupted
  • Use the isInterrupted method
  • An interrupted thread should release resources,
    clean up, and exit
  • The sleep method throws an InterruptedException
    when a sleeping thread is interrupted
  • Catch the exception
  • Terminate the thread

14
Terminating a Thread
  • public void run( )
  • try
  • for (int 1
  • i lt REPETITIONS !isInterrupted()
  • i)
  • //do the work
  • catch (InterruptedException exception)
  • //handling the exception
  • //cleanup

15
Corrupting Data with Unsynchronized Threads
  • When threads share a common object, they can
    conflict with each other.
  • In this example, a DepositThread and a
    WithdrawThread both manipulate a single
    BankAccount

16
Sample Application
  • Create a BankAccount object
  • Create a DepositThread t0 to deposit 100 into
    the account for 10 iterations
  • Create a WithdrawThread t1 to withdraw 100 from
    the account for 10 iterations
  • The result should be zero, but sometimes it is
    not

17
Scenario to Explain Non-zero Result
  • The first thread t0 executes the lines
    System.out.print("Depositing " amount)
  • double newBalance balance amount
  • t0 reaches the end of its time slice and t1 gains
    control
  • t1 calls the withdraw method which withdraws 100
    from the balance variable.
  • Balance is now -100
  • t1 goes to sleep

18
Scenario to Explain Non-zero Result (cont.)
  • t0 regains control and picks up where it left
    off.
  • t0 executes the lines
  • System.out.println(", new balance is "
    newBalance)
  • balance newBalance
  • The balance is now 100 instead of 0 because the
    deposit method used the OLD balance
  • This is called a race condition.

19
Race condition
  • Occurs if the effect of multiple threads on
    shared data depends on the order in which the
    threads are scheduled
  • It is possible for a thread to reach the end of
    its time slice in the middle of a statement.
  • It may evaluate the right-hand side of an
    assignment but not be able to store the result
    until its next turn.

20
File BankAccountThreadTest.java
  • /
  • This program runs two threads that deposit and
  • withdraw money from the same bank account.
  • /
  • public class BankAccountThreadTest
  • public static void main(String args)
  • BankAccount account new BankAccount()
  • DepositThread t0
  • new DepositThread(account, 100)
  • WithdrawThread t1
  • new WithdrawThread(account, 100)
  • t0.start()
  • t1.start()

21
File DepositThread.java
  • /
  • A deposit thread makes periodic deposits to a
    bank account.
  • /
  • class DepositThread extends Thread
  • /
  • Constructs a deposit thread.
  • _at_param anAccount the account into which to
  • deposit money
  • _at_param anAmount the amount to deposit in
    each
  • repetition
  • /
  • public DepositThread( BankAccount anAccount,
  • double anAmount)
  • account anAccount
  • amount anAmount

22
  • public void run()
  • try
  • for (int i 1
  • i lt REPETITIONS
    !isInterrupted()
  • i)
  • account.deposit(amount)
  • sleep(DELAY)
  • catch (InterruptedException exception)
  • private BankAccount account
  • private double amount
  • private static final int REPETITIONS 10
  • private static final int DELAY 10
  • / end class /

23
run Method of DepositThread
  • public void run()
  • try
  • for (int i 1
  • i lt REPETITIONS !isInterrupted()
  • i)
  • account.deposit(amount)
  • sleep(DELAY)
  • catch (InterruptedException exception)

24
File WithdrawThread.java
  • /
  • A withdraw thread makes periodic withdrawals
    from a bank account.
  • /
  • class WithdrawThread extends Thread
  • /
  • Constructs a withdraw thread.
  • _at_param anAccount the account from which to
  • withdraw money
  • _at_param anAmount the amount to withdraw in
    each
  • repetition
  • /
  • public WithdrawThread(BankAccount anAccount,
  • double anAmount)
  • account anAccount
  • amount anAmount

25
  • public void run()
  • try
  • for (int i 1
  • i lt REPETITIONS !isInterrupted()
  • i)
  • account.withdraw(amount)
  • sleep(DELAY)
  • catch (InterruptedException exception)
  • private BankAccount account
  • private double amount
  • private static final int REPETITIONS 10
  • private static final int DELAY 10

26
File BankAccount.java
  • /
  • A bank account has a balance that can be
    changed by
  • deposits and withdrawals.
  • /
  • public class BankAccount
  • /
  • Constructs a bank account with a zero
    balance
  • /
  • public BankAccount()
  • balance 0
  • /
  • Deposits money into the bank account.
  • _at_param amount the amount to deposit
  • /

27
  • public void deposit(double amount)
  • System.out.print("Depositing " amount)
  • double newBalance balance amount
  • System.out.println(", new balance is "
    newBalance)
  • balance newBalance
  • /
  • Withdraws money from the bank account.
  • _at_param amount the amount to withdraw
  • /
  • public void withdraw(double amount)
  • System.out.print("Withdrawing " amount)
  • double newBalance balance - amount
  • System.out.println(", new balance is "
    newBalance)
  • balance newBalance

28
  • /
  • Gets the current balance of the bank
    account.
  • _at_return the current balance
  • /
  • public double getBalance()
  • return balance
  • private double balance

29
Sometimes ...
The final balance is not zero! The data is
corrupted!
Write a Comment
User Comments (0)
About PowerShow.com