Professional Documents
Culture Documents
Chapter 16 - Multithreading: Runnable Interface
Chapter 16 - Multithreading: Runnable Interface
Outline
16.1 Introduction
16.2 Thread States: Life Cycle of a Thread
16.3 Thread Priorities and Thread Scheduling
16.4 Creating and Executing Threads
16.5 Thread Synchronization
16.6 Producer/Consumer Relationship without Synchronization
16.7 Producer/Consumer Relationship with Synchronization
16.8 Producer/Consumer Relationship: Circular Buffer
16.9 Daemon Threads
16.10 Runnable Interface
16.11 (Optional Case Study) Thinking About Objects:
Multithreading
16.12 (Optional) Discovering Design Patterns: Concurrent Design
Patterns
• Thread states
– Born state
• Thread was just created
– Ready state
• Thread’s start method invoked
• Thread can now execute
– Running state
• Thread is assigned a processor and running
– Dead state
• Thread has completed or exited
• Eventually disposed of by system
start
Ready
thread dispatch
quantum expiration (assign a
yield
timeout expires
processor)
I/O completes
interrupt
notifyAll
interrupt
acquire lock
notify
Running
Thread.MAX_PRIORITY Priority 10 A B
Priority 9 C
Priority 8
Priority 7 D E F
Priority 6 G
Thread.NORM_PRIORITY Priority 5 H I
Priority 4
Priority 3
Priority 2 J K
Thread.MIN_PRIORITY Priority 1
• Sleep state
– Thread method sleep called
– Thread sleeps for a set time interval then awakens
Starting threads
Threads started, main ends
Producer writes 1
Producer writes 2
Consumer reads 2
Producer writes 3
Consumer reads 3
Producer writes 4
Producer done producing.
Terminating Producer.
Consumer reads 4
Consumer reads 4
Consumer read values totaling: 13.
Terminating Consumer.
Lines 25-26
Operation Buffer Occupied Count
Initial State -1 0
Producer writes 1 1 1
Consumer reads 1 1 0
Producer writes 2 2 1
Consumer reads 2 2 0
Producer writes 3 3 1
Consumer reads 4 4 0
Producer done producing.
Terminating Producer.
Initial State -1 0
Producer writes 1 1 1
Consumer reads 1 1 0
Producer writes 2 2 1
Consumer reads 3 3 0
Producer writes 4 4 1
Initial State -1 0
Producer writes 1 1 1
Consumer reads 1 1 0
Producer writes 2 2 1
CircularBufferT
est.java
CircularBufferT
est.java
CircularBufferT
est.java
CircularBufferT
est.java
requestElevator( )
*[ !floorDoor.isDoorOpen() ] wait( )
[Elevator on opposite
Floor of request]
elevator travels
to other floor
elevatorArrived( )
openDoor( )
openDoor( )
setLocation( elevator )
Sequence
continues in
Fig. 16.18
pressButton( ) buttonPressed( )
setMoving( true )
Sequence
continued from
Fig. 16.17
closeDoor( )
== 5
*[ !elevatorDoor.isDoorOpen() ] wait( ) seconds
setMoving( false )
changeFloors()
elevatorArrived( )
openDoor( )
setLocation( elevator.getCurrentFloor() )
Signals
arrival 1
1
1
ElevatorShaft
Signals
1 arrival
1
Informs of
pressing
Closes Resets
2 1
1 2 Presses
Door Button Person
1 Signals 1 1
1 arrival 1
1 1 1 1
Waits for
opening
Opens/Closes
1 1
1 1
Signals
to move Occupies
ElevatorDoor Elevator
1 Signals
1 1 Signals 1 1 arrival
arrival
Opens/Closes Travels to
Signals
arrival 2 1
Rings
1
Location
1
1 Bell
Bell
Elevator + ringBell( ) : void
- moving : Boolean = false
- summoned : Boolean = false
- currentFloor : Location Button
- destinationFloor : Location - pressed : Boolean = false
- travelTime : Integer = 5
+ resetButton( ) : void
+ pressButton( ) : void
+ ride( ) : void
+ requestElevator( ) : void
– setMoving( Boolean ) : void Door
+ getButton( ) : Button - open : Boolean = false
+ getDoor( ) : Door
+ getLocation( ) : Location + openDoor( ) : void
+ closeDoor( ) : void