Concurrent Programming in Java - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

Concurrent Programming in Java

Description:

public final void resume(); public final synchronized void stop ... Example 1: suspend/resume in Java 1 (cont'd) ... out.println('Resuming thread One'); ob2. ... – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 36
Provided by: AkosLe8
Category:

less

Transcript and Presenter's Notes

Title: Concurrent Programming in Java


1
Concurrent Programmingin 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
E2
E1
action2
action1
action3
E3
Properties - direct event action
association - independent, parallel execution
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) gtgt
numberOf(processors) ? processor sharing ?
scheduling
6
Process/thread states
running
scheduler decision
wait for event
yield
blocked
runnable
event
(running) 0 .. number of processors (runnable)
0 .. arbitrary
7
Schedulers 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 gt
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 gt 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 gt
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 gt 0 i--) System.out.println("Mai
n Thread " i) Thread.sleep(1000)
catch (InterruptedException e)
System.out.println("Main thread interrupted.")
System.out.println("Main thread
exiting.")
constructor for the new thread class
thread 1
thread 2
start new thread
just wait for a while
13
Interprocess communication
Common resources the problem
critical region
thread 2 . . i21 i22 i23 i24
. .
thread 1 . . i11 i12 i13 i14 .
.
R
For certain resources access must be mutually
exclusive
14
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
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(InterruptedExcepti
on 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
(contd)
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(InterruptedExcepti
on 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 ) ltstatements to be
synchronizedgt 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(...) ltBODYgt
class Whatever . ReturnType
method(...) synchronized (this) ltBODYgt
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 (contd) java.lang.Thread p
ublic 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 (contd) java.lang.Thread p
ublic final void suspend() public final void
resume() public final synchronized void
stop(Throwable o) public final void stop()
26
Thread state transitions in Java 1.1 and earlier
new
new
start
runnable
dead
stop
resume
yield, time slice
notify, notifyAll, IO compl, sleep exp, join
compl.
scheduler
stop
suspend
stop, term
stop
suspended
running
blocked
suspend
IO, sleep, wait, join
resume
IO compl.
suspend
blocked- susp.
27
Thread state transitions in Java 1.2
new
new
start
runnable
yield, time slice
notify, notifyAll, IO compl, sleep exp, join
compl.
scheduler
running
blocked
IO, sleep, wait, join
term
dead
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 gt 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 (contd)
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("Suspendin
g 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
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 gt 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/resume in Java 2
32
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("Suspend
ing 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("Mai
n thread exiting.")
Example 2 suspend/resume in Java 2 (contd)
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
Usage
Thread 1 semaphore s1 1 / to protect R1
resource initial value free
/ . . P(s1) / lock R1 if available
/ ltoperations on R1gt V(s1) / free R1
/ . .
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
Write a Comment
User Comments (0)
About PowerShow.com