Threads in Java - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Threads in Java

Description:

Implementation of the Runnable interface. These execute the run() method ... public class Courier implements Runnable. public void run () { for(int i = 0; i ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 19
Provided by: ottarvik
Category:
Tags: java | runnable | threads

less

Transcript and Presenter's Notes

Title: Threads in Java


1
  • Threads in Java
  • Mutual exclusion in Java

2
Threads Concept
  • A Thread is a flow of execution, with a beginning
    and an end, of a task in a program.
  • Multiprocessor systems multiple threads can be
    executed simultaneously.
  • Single-processor systems
  • Threads share CPU time.
  • The operating system is responsible for
    scheduling and allocating resources to them.

3
(No Transcript)
4
Threads Concept
Multiple threads on multiple CPUs
Multiple threads sharing a single CPU
5
Thread States
6
Thinking about threads
  • A thread propagates through your program
    executing statements.
  • At a given point of time, every thread (that you
    made) is somewhere in your program.
  • When you have many threads, different parts of
    your program are executed simultaneously
  • In reality, the different threads statements are
    interleaved.

7
Threads in Java
  • A thread is a strand of execution.
  • The main thread
  • Executes the main() method
  • The thread ends when the main() method returns.
  • User-made threads
  • Thread subclass
  • Implementation of the Runnable interface
  • These execute the run() method
  • The threads end when the run() method returns.
  • The event handler thread
  • Started by the API when a GUI is created
  • The garbage collection thread
  • Started by the JVM

8
Creating a thread
  • When you start a program you start the main
    thread.
  • To start more threads

public class Courier extends Thread
public void run () for(int i 0 i lt
nofVisits i) bank.depositMoney(100)

public static void main(String args)
Courier courier new Courier()
courier.start() for(int i 0 i lt 15 i)
System.out.println(Blah)
  • At the courier.start() statement the executing
    thread (the main thread) branches into two
    threads, indicated by the blue (main) and red
    (courier) arrow.

9
  • Equivalent method

public class Courier implements Runnable
public void run () for(int i 0 i lt
nofVisits i) bank.depositMoney(100)

public static void main(String args)
Courier courier new Courier() Thread t new
Thread(courier) t.start() for(int i 0 i
lt 15 i) System.out.println(Blah)
10
What is mutual exclusion?
  • Mutual exclusion is about guaranteeing that a
    resource is only accessed by one user at a time.
  • In our case, the resource is a code segment, and
    the users are threads.
  • Code that must be mutually exclusive is called
    critical code.

11
When is code critical?
  • When multiple threads may execute a code segment,
    and
  • the code segment performs some operation that
    consists of multiple statements but must be
    treated as an atomic operation.
  • Similar to a transaction in database systems.
  • Violation of the mutual exclusion requirement can
  • lead to data inconsistensies.
  • Example (critical code marked with red)

public class Money private int balance
1000 public void addMoney(int t) int
temp balance temp t balance
temp
12
  • The three statements in the addMoney() method are
    critical code. Example of serial execution

Statement executed by thread 1 Statement executed by thread 2 Value of Variable (temp),balance after this statement
addMoney(50) tempbalance temp t balance temp 1 (0),1000 1(1000),1000 1(1050),1000 1(1050),1050
addMoney(50) tempbalance temp t balance temp 2(0),1050 2(1050),1050 2(1100), 1050 2(1100), 1100
13
  • The three statements in the addMoney() method are
    critical code. Example of what could go wrong if
    the code isnt treated as an atomic unit

Statement executed by thread 1 Statement executed by thread 2 Value of Variable (temp),balance after this statement
addMoney(50) 1(0), 1000
temp balance temp t 1(1000),1000 1(1050),1000
addMoney(50) 2(0), 1000
temp balance temp t balance temp 2(1000),1000 2(1050),1000 2(1050),1050
balance temp 1(1050),1050
14
  • We see from the example that the variables
    balance gets wrong value (they should sum to
    1100) because two threads are modifying them
    simultaneously.
  • Solutions
  • Write smarter code.
  • Protect critical sections of the code with mutual
    exclusion.
  • Code where these solutions have been applied is
    called threadsafe because it is safe to have
    multiple threads executing the code.

15
Avoiding critical code
  • In some cases it is possible to perform
    operations in such a way that interleaving of
    statements is not a problem.
  • Example

public class Money private int balance
1000 public void addMoney(int t)
balance t
16
Enforcing mutual exclusion
  • If critical code is unavoidable, mutual exclusion
    must be enforced with locks.
  • In Java, this is done using the synchronized
    keyword.

public class Money private int balance
1000 public synchronized void addMoney(int
t) int temp balance temp t
balance temp
17
The synchronized keyword
18
The synchronized keyword
  • Used to indicate that a thread must acquire a
    lock before proceeding.
  • When the thread leaves the section of the code
    marked as synchronized, it releases the lock,
    allowing waiting threads to enter.
  • Only one thread can have the lock at any time, so
    only one thread can be inside a synchronized
    block at any time.
  • Different synchronized blocks may use the same
    lock.
  • More on the synchronized keyword in exercise 2.
Write a Comment
User Comments (0)
About PowerShow.com