Title: INFT316 Concurrency I
1INFT316Concurrency I
http//www-dse.doc.ic.ac.uk/concurrency/
Using material from Magee and Kramer
2Concurrency
- Advantages of concurrency
- Performance gain
- parallelism
- Increased application throughput
- an I/O call need only block one thread
- Increased application responsiveness
- prioritised threads
- More appropriate structure
- for programs which interact with the environment,
control multiple activities and handle multiple
events
- A sequential program has a single thread of
control. - A concurrent program has multiple threads of
control allowing it perform multiple computations
in parallel and to control multiple external
activities which occur at the same time.
3Concurrent Processes
- Structure complex systems as sets of simpler
activities, represented as sequential processes - Definition process/thread
- - sequence of actions
- Interaction between concurrent processes can be
very complex - gt models of concurrent behaviour useful
- Modelling Concurrent Processes
- Graphically Labelled Transition System (e.g.,
FSA) - Algebraically Process Algebras (e.g., CSP, FSP)
- Implementing Concurrent Processes in Java
- class Thread
- interface Runnable
4Modelling Processes
- Process is the execution of a sequential program
- modelled as a finite state machine which transits
from state to state by executing a sequence of
atomic actions - E.g., a light switch
Labelled Transition System
Sequence of actions or trace
on?off?on?off?on?off?
5Process Algebra Notation
- If x is an action and P a process then (x-gt P)
describes a process that initially engages in the
action x and then behaves exactly as described by
P - Convention actions begin with lowercase,
processes with uppercase - Special process called STOP
- ONCE_OFF (once -gt STOP).
- Using recursion
- SWITCH (on-gtoff-gtSWITCH).
- TRAFFICLIGHT
- (green-gtorange-gtred-gtTRAFFICLIGHT).
6Choice
- ( x -gt P y -gt Q)
- initially engages in either x or y
- if x, then behaves as P
- if y, then behaves as Q
- E.g.,
- DRINKS ( red-gtcoffee-gtDRINKS
- blue-gttea-gtDRINKS).
- Non-deterministic choice
- ( x -gt P x -gt Q)
- engages in x, then behaves as either P or Q
- E.g.,
- COIN ( toss -gt heads -gt COIN
- toss -gt tails -gt COIN ).
blue
toss
red
toss
0
1
2
0
1
2
coffee
heads
tea
tails
7Guarded Actions
- (when B x -gt P y -gt Q)
- when guard B is true, x and y are available
actions - otherwise, x is not enabled
- E.g.,
- COUNTi0..N
- (when (iltN) inc-gtCOUNTi1
- when (igt0) dec-gtCOUNTi -1).
- COUNTDOWN (N3)
- start -gt COUNTDOWN N
- COUNTDOWN i0..N
- ( when (i gt 0) tick-gtCOUNTDOWNi 1
- when (i0) beep-gtSTOP
- stop-gtSTOP
- ).
inc
inc
0
1
2
dec
dec
8Threads in Java
- Class Thread manages a sequential thread of
control - threads may be created and deleted dynamically
- thread object executes instructions from its
method run( )
- Interface Runnable preferred
- interface Runnable
- public abstract void run( )
-
- class MyRun implements Runnable
- public void run( )
-
-
-
- Thread t new Thread(new MyRun( ))
class MyThread extends Thread public void run
( )
Thread t new MyThread( )
9Thread Life-cycle in Java
new Thread()
start( ) causes the thread to call its run()
method.
start()
Created
Alive
stop(), or run() returns
stop()
Terminated
The predicate isAlive() can be used to test if a
thread has been started but not terminated. Once
terminated, it cannot be restarted.
10Thread Alive States in Java
start()
Running
sleep()
suspend()
yield()
dispatch
suspend()
Runnable
Non-Runnable
resume()
stop(), or run() returns
11Process Algebra Specification of Thread Life-cycle
- THREAD CREATED
- CREATED (start-gtRUNNING
stop-gtTERMINATED) - RUNNING
- ( suspend,sleep-gtNON_RUNNABLE
- yield-gtRUNNABLE
- stop,end-gtTERMINATED
- run-gtRUNNING)
- RUNNABLE
- ( suspend-gtNON_RUNNABLE
- dispatch-gtRUNNING
- stop-gtTERMINATED)
- NON_RUNNABLE ( resume-gtRUNNABLE
stop-gtTERMINATED) - TERMINATED STOP
12Threads Example
- class RepeatedMessage extends Thread
- private String message
- private int pause
- private volatile boolean stopFlag
- public RepeatedMessage(String m, int p)
- message m pause p stopFlag true
-
- public static void displayMessage(
RepeatedMessage rm ) - throws InterruptedException
- for (int i 0 i lt rm.message.length() i)
- System.out.print(rm.message.charAt(i))
- sleep(50)
-
- System.out.println()
-
- public void run()
- stopFlag false
- try
- while (!stopFlag)
- displayMessage(this)
- sleep(pause)
-
- catch (InterruptedException ie) return
-
- public void finish() stopFlag true
13Threads Example
- public static void main(String args)
- try
- RepeatedMessage m1
- new RepeatedMessage("Bon jour!", 1)
- RepeatedMessage m2
- new RepeatedMessage("Guten Tag!", 500)
- m1.start()
- m2.start()
- sleep(5000)
- m1.finish()
- m2.finish()
- catch (InterruptedException ie)
ie.printStackTrace() -
Sample output BGount ejno uTra!g !B on
jour! BoGnu tjeonu rT!a Bgo!n jour! BonG
ujtoeunr !T Baogn! jour! BGount ejno
uTra!g B!o n jouGru!t en Tag!
14Graceful Termination
- Dont use stop( ) to finish a thread
- let a thread finish its normal processing
- protects integrity of internal state
- For continuously looping threads, provide some
loop control so that an outside agent can
indicate its time for the thread to finish
public void run() stopFlag false
try while (!stopFlag)
displayMessage(this) sleep(pause)
catch (InterruptedException ie) return
public void finish() stopFlag true
15Interleaving
- Concurrency implies logically simultaneous
processing - does not require multiple processors
- requires (at least) interleaved execution on a
single processor - Parallelism implies physically simultaneous
processing
A
B
C
- Requires controlled access to shared resources
16Prelude to Locking
public synchronized static void displayMessage(
RepeatedMessage rm ) throws InterruptedException
for (int i 0 i lt rm.message.length()
i) System.out.print(rm.message.charAt(i)
) sleep(50) System.out.println()
- Consider possible traces of RepeatedMessage
- Java keyword synchronized
- provides mutually exclusive access to a region of
code - enforces certain restrictions on possible traces
- (reduces concurrency)
Bon jour! Guten Tag! Bon jour! Guten Tag! Bon
jour! Bon jour! Guten Tag!