Upload
clarence-booth
View
227
Download
2
Tags:
Embed Size (px)
Citation preview
Motivation: event driven, responsive systems
Sequential approach:
while ( true ) { do event = getEventId() while ( event == null ); switch ( event ) { case E1: action1(); break; case E2: action2(); break; . . }}
Properties: - not a “nice” structure (e.g. extendibility)- not always responsive
Parallel approach:
Properties: - direct event – action association- independent, parallel execution
action1
action3
action2E1
E2
E3
New concepts
Process/thread/tasksingle chain of instruction execution
Interprocess communicationshare common resourcessynchronization
Concurrent programming:introduces these concepts in programming languages (i.e. new programming primitives)investigates how to build compound systems safely
Processes/threads/tasks
Main conflict:
numberOf(threads) >> numberOf(processors)
processor sharing scheduling
running
blockedrunnable
Process/thread states:
“yield”
“wait for event”
“event”
schedulerdecision
#(running) = 0 .. number of processors#(runnable) = 0 .. arbitrary
Scheduler’s main operations:
save thread contextrestore thread contextpass control to threadselect thread to run: policy!
Scheduling schemes
Aspect 1: thread selectionpriority basedround-robinFIFOor any combination of these...
Aspect 2: moments of event evaluationnon-preemptive (“cooperative scheduling”)preemptive
Java threads
java.jang package:
public class Threadextends Object implements Runnable
public abstract interface Runnable
For details check the class documentation!
Java thread scheduling:
priority basedinside priority groups: round-robinpreemptivetime slicing: platform dependent – not guaranteed!
Java thread definition 1: implementing the Runnable interface
class NewThread implements Runnable {
public void run() { try { for(int i = 5; i > 0; i--) { System.out.println("Child Thread: " + i); Thread.sleep(500); } } catch (InterruptedException e) { System.out.println("Child interrupted."); } System.out.println("Exiting child thread."); }}
public class TD1 {
public static void main(String args[]) {
Thread t = new Thread(new NewThread(), "Child Thread");
t.start(); try { for(int i = 5; i > 0; i--) { System.out.println("Main Thread: " + i); Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println("Main thread interrupted."); } System.out.println("Main thread exiting."); }}
thread 1
thread 2
start newthread
just wait for a while
Java thread definition 2: extending the Thread class
class NewThread extends Thread {
NewThread(String name) { super(name); }
public void run() { try { for(int i = 5; i > 0; i--) { System.out.println("Child Thread: " + i); Thread.sleep(500); } } catch (InterruptedException e) { System.out.println("Child interrupted."); } System.out.println("Exiting child thread."); }}
public class TD2 { public static void main(String args[]) { Thread t = new NewThread("Child Thread"); // create a new thread
t.start(); try { for(int i = 5; i > 0; i--) { System.out.println("Main Thread: " + i); Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println("Main thread interrupted."); } System.out.println("Main thread exiting."); }}
thread 1
thread 2
start newthread
just wait for a while
constructor for the new thread class
Interprocess communication
Common resources: the problem
thread 1: . . i11; i12; i13; i14; . .
thread 2: . . i21; i22; i23; i24; . .
R
For certain resources: access must be mutually exclusive
criticalregion
Mutual exclusion rule for protected resources:
Let C denote the union of critical regions ci for resource R. T is the set of threads tj having access to R. The mutual exclusive access to R is satisfied if at any time instance only at most one tj T executes ck C.
On programming language level: explicit notation for critical regions.On runtime system level: implementing the rule
Example 1: Not synchronized resource access
// This program is not synchronized.class Callme { void call(String msg) { System.out.print("[" + msg); try { Thread.sleep(1000); } catch(InterruptedException e) { System.out.println("Interrupted"); } System.out.println("]"); }}
class Caller implements Runnable { String msg; Callme target; Thread t;
public Caller(Callme targ, String s) { target = targ; msg = s; t = new Thread(this); t.start(); }
public void run() { target.call(msg); }}
Example 1: Not synchronized resource access (cont’d)
class Synch1 { public static void main(String args[]) { Callme target = new Callme(); Caller ob1 = new Caller(target, "Hello"); Caller ob2 = new Caller(target, "Synchronized"); Caller ob3 = new Caller(target, "World");
// wait for threads to end try { ob1.t.join(); ob2.t.join(); ob3.t.join(); } catch(InterruptedException e) { System.out.println("Interrupted"); } }}
Mutual exclusion in Java
Synchronized methods:
class Whatever { . . synchronized ReturnType method(...) { . }
}
Operation: synchronized method executions are serialized
Example 2: Synchronized resource access
// This program is not synchronized.class Callme { synchronized void call(String msg) { System.out.print("[" + msg); try { Thread.sleep(1000); } catch(InterruptedException e) { System.out.println("Interrupted"); } System.out.println("]"); }}
class Caller implements Runnable { String msg; Callme target; Thread t;
public Caller(Callme targ, String s) { target = targ; msg = s; t = new Thread(this); t.start(); }
public void run() { target.call(msg); }}
Synchronized statements:
. . synchronized ( object ) { <statements to be synchronized> }
Operation: “lock” is associated with object
Example 3: Resource access via synchronized statements
// The code is the same as that of Example 1 - except the// ‘run’ method body
class Caller implements Runnable { String msg; Callme target; Thread t;
public Caller(Callme targ, String s) { target = targ; msg = s; t = new Thread(this); t.start(); }
// synchronize calls to call() public void run() { synchronized(target) { // synchronized block target.call(msg); } }}
Important: NO MAGIC!
class Whatever {
.
synchronized ReturnType method(...) {
<BODY>
}
}
class Whatever {
.
ReturnType method(...) {
synchronized (this) { <BODY> }
}
}
THE TWO SOLUTIONS ARE EQUIVALENT!
Thread synchronization in Java
Implementation: via special operations (invoked by threads)
java.lang.Object:
public final native void wait(long timeout)throws InterruptedException;
public final void wait()throws InterruptedException;
public final native void notify();
public final native void notifyAll();
The “fine details”: direct control of thread state transitions
java.lang.Thread:
public static final int MAX_PRIORITY;public static final int MIN_PRIORITY;public static final int NORMAL_PRIORITY;
public static native void sleep(long millis)throws InterruptedException;
public static boolean interrupted();
public static native void yield();
The “fine details” (cont’d)
java.lang.Thread:
public final int getPriority();public final void setPriority();
public void interrupt();public boolean isInterrupted();
public final synchronized void join(long millis)throws InterruptedException;
public void join()throws InterruptedException;
The “fine details” (cont’d)
java.lang.Thread:
public final void suspend();public final void resume();
public final synchronized void stop(Throwable o);public final void stop();
runnable
scheduler
Thread state transitions in Java 1.1 and earlier
new
dead
suspended running blocked
blocked-susp.
new
stop
start
stop
stopstop, term
resume
suspend
suspend
resume
suspend
IO, sleep,wait, join
yield,time slice
notify, notifyAll,IO compl, sleep exp,
join compl.
IO compl.
Thread state transitions in Java 1.2
runnable
scheduler
new
dead
running blocked
new
start
term
IO, sleep,wait, join
yield,time slice
notify, notifyAll,IO compl, sleep exp,
join compl.
Reasons for the “clean-up”: stop() and suspend() are inherently unsafe!
- they encourage “messy” program structures- coherent “lock” states are difficult (or impossible) to maintain- they can result in corrupted data
Example 1: suspend/resume in Java 1
class NewThread implements Runnable { String name; // name of thread Thread t;
NewThread(String threadname) { name = threadname; t = new Thread(this, name); System.out.println("New thread: " + t); t.start(); // Start the thread }
// This is the entry point for thread. public void run() { try { for(int i = 15; i > 0; i--) { System.out.println(name + ": " + i); Thread.sleep(200); } } catch (InterruptedException e) { System.out.println(name + " interrupted."); } System.out.println(name + " exiting."); }}
Example 1: suspend/resume in Java 1 (cont’d)class SusRes1 { public static void main(String args[]) { NewThread ob1 = new NewThread("One"); NewThread ob2 = new NewThread("Two");
try { Thread.sleep(1000); ob1.suspend(); System.out.println("Suspending thread One"); Thread.sleep(1000); ob1.resume(); System.out.println("Resuming thread One"); ob2.suspend(); System.out.println("Suspending thread Two"); Thread.sleep(1000); ob2.resume(); System.out.println("Resuming thread Two"); } catch (InterruptedException e) { System.out.println("Main thread Interrupted"); }
// wait for threads to finish try { System.out.println("Waiting for threads to finish."); ob1.t.join(); ob2.t.join(); } catch (InterruptedException e) { System.out.println("Main thread Interrupted"); } System.out.println("Main thread exiting."); }}
Example 2:suspend/resumein Java 2
class NewThread implements Runnable { String name; // name of thread Thread t; boolean suspendFlag; NewThread(String threadname) { name = threadname; t = new Thread(this, name); System.out.println("New thread: " + t); suspendFlag = false; t.start(); // Start the thread }
public void run() { try { for(int i = 15; i > 0; i--) { System.out.println(name + ": " + i); Thread.sleep(200); synchronized(this) { while(suspendFlag) wait();} } } catch (InterruptedException e) { System.out.println(name + " interrupted."); } System.out.println(name + " exiting."); }
void mysuspend() { suspendFlag = true; }
synchronized void myresume() { suspendFlag = false; notify(); }}
Example 2:suspend/resumein Java 2(cont’d)
class SusRes2 { public static void main(String args[]) { NewThread ob1 = new NewThread("One"); NewThread ob2 = new NewThread("Two");
try { Thread.sleep(1000); ob1.mysuspend(); System.out.println("Suspending thread One"); Thread.sleep(1000); ob1.myresume(); System.out.println("Resuming thread One"); ob2.mysuspend(); System.out.println("Suspending thread Two"); Thread.sleep(1000); ob2.myresume(); System.out.println("Resuming thread Two"); } catch (InterruptedException e) { System.out.println("Main thread Interrupted"); }
// wait for threads to finish try { System.out.println("Waiting for threads to finish."); ob1.t.join(); ob2.t.join(); } catch (InterruptedException e) { System.out.println("Main thread Interrupted"); } System.out.println("Main thread exiting."); }}
Semaphores (locks):
resource lock
Implementation: primitive data type + operations (atomic)
E.g. semaphore + P, V operations
V(s): s := s + 1;P(s): if s == 0 then wait_until( s 0 ); s := s - 1;
Thread 1:
semaphore s1 = 1; /* to protect R1 resource initial value: free */
.
.P(s1); /* lock R1 if available */<operations on R1>V(s1); /* free R1 */..
Usage:
Example: Semaphores in Java
public class Semaphore {
long sem;
public Semaphore(long init) { sem = init; }
public synchronized void P() throws InterruptedException { while ( sem == 0 ) wait(); sem -= 1; }
public synchronized void V() { sem += 1; notifyAll(); } public boolean isDown() { return sem == 0; }
}