Presentation is loading. Please wait.

Presentation is loading. Please wait.

Concurrent Programming in Java Dr. Zoltan Papp. Motivation: event driven, responsive systems Sequential approach: while ( true ) { do event = getEventId()

Similar presentations


Presentation on theme: "Concurrent Programming in Java Dr. Zoltan Papp. Motivation: event driven, responsive systems Sequential approach: while ( true ) { do event = getEventId()"— Presentation transcript:

1 Concurrent Programming in Java Dr. Zoltan Papp

2 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

3 Parallel approach: Properties:- direct event – action association - independent, parallel execution action1 action3 action2 E1 E2 E3

4 New concepts Process/thread/task single chain of instruction execution Interprocess communication share common resources synchronization Concurrent programming: introduces these concepts in programming languages (i.e. new programming primitives) investigates how to build compound systems safely

5 Processes/threads/tasks Main conflict: numberOf(threads) >> numberOf(processors)  processor sharing  scheduling

6 running blockedrunnable Process/thread states: “yield” “wait for event” “event” scheduler decision #(running) = 0.. number of processors #(runnable) = 0.. arbitrary

7 Scheduler’s main operations: save thread context restore thread context pass control to thread select thread to run: policy!

8 Scheduling schemes Aspect 1: thread selection priority based round-robin FIFO or any combination of these... Aspect 2: moments of event evaluation non-preemptive (“cooperative scheduling”) preemptive

9 Java threads java.jang package: public class Thread extends Object implements Runnable public abstract interface Runnable For details check the class documentation!

10 Java thread scheduling: priority based inside priority groups: round-robin preemptive time slicing: platform dependent – not guaranteed!

11 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 new thread just wait for a while

12 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 new thread just wait for a while constructor for the new thread class

13 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 critical region

14 Mutual exclusion rule for protected resources: Let C denote the union of critical regions c i for resource R. T is the set of threads t j having access to R. The mutual exclusive access to R is satisfied if at any time instance only at most one t j  T executes c k  C. On programming language level: explicit notation for critical regions. On runtime system level: implementing the rule

15 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); }

16 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"); }

17 Mutual exclusion in Java Synchronized methods: class Whatever {. synchronized ReturnType method(...) {. } Operation: synchronized method executions are serialized

18 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); }

19 Synchronized statements:. synchronized ( object ) { } Operation: “lock” is associated with object

20 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); }

21 Important: NO MAGIC! class Whatever {. synchronized ReturnType method(...) { } class Whatever {. ReturnType method(...) { synchronized (this) { } } THE TWO SOLUTIONS ARE EQUIVALENT!

22 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();

23 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();

24 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;

25 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();

26 runnable scheduler Thread state transitions in Java 1.1 and earlier new dead suspendedrunningblocked blocked- susp. new stop start stop stop, term resume suspend resume suspend IO, sleep, wait, join yield, time slice notify, notifyAll, IO compl, sleep exp, join compl. IO compl.

27 Thread state transitions in Java 1.2 runnable scheduler new dead runningblocked new start term IO, sleep, wait, join yield, time slice notify, notifyAll, IO compl, sleep exp, join compl.

28 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

29 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."); }

30 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."); }

31 Example 2: suspend/resume in 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(); }

32 Example 2: suspend/resume in 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."); }

33 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;

34 Thread 1: semaphore s1 = 1; /* to protect R1 resource initial value: free */. P(s1);/* lock R1 if available */ V(s1);/* free R1 */. Usage:

35 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; }


Download ppt "Concurrent Programming in Java Dr. Zoltan Papp. Motivation: event driven, responsive systems Sequential approach: while ( true ) { do event = getEventId()"

Similar presentations


Ads by Google