Transcript slides

cs205: engineering software
university of virginia
fall 2006
Concurrency
without Locks
Kramer Sharp
cs205: engineering software
1
Recap
• synchronized(obj) { code }
– Provides mutual exclusion: code inside
synchronized can only run when lock of
obj is held
• obj.wait(): Gives up lock on obj; puts
current thread in waiting set for obj
• obj.notify(), obj.notifyAll(): Don’t give up
lock; selects one (notify) or all
(notifyAll) threads in waiting set for obj
and wakes them up (to be scheduled)
cs205: engineering software
2
Thread A
wait and notify
Thread B
synchronized (o)
o.wait ()
synchronized (o) {
waiting
o.notify ()
awake, but not running
can reclaim o lock
cs205: engineering software
} // end synchronized
3
Thread A
synchronized (o)
wait and notify
Thread B
Thread C
synchronized (o)
o.wait ()
synchronized (o) {
waiting
o.wait ()
waiting
If multiple
threads are waiting on the
same object, any one of
them can be awakened
o.notify ()
still
waiting
cs205: engineering software
} // end synchronized
awake,
not running
4
class IncThread extends Thread {
private Counter c;
public IncThread (Counter p_c) { c = p_c; }
public void run () {
while (true) {
synchronized (c) {
c.increment ();
System.err.println ("Running inc thread: " + currentThread () + …);
c.notify ();
}}}}
class DecThread extends Thread {
…
public void run () {
while (true) {
synchronized (c) {
while (c.getValue () <= 0) {
try { c.wait (); } catch (InterruptedException e) { ; }
}
c.decrement ();
System.err.println ("Running dec thread: " + …);
}}}}
cs205: engineering software
5
Counter c = new Counter ();
IncThread ithread = new IncThread (c);
DecThread dthread = new DecThread (c);
ithread.setPriority (Thread.NORM_PRIORITY);
ithread.start ();
dthread.setPriority (Thread.MAX_PRIORITY);
dthread.start ();
Running inc thread: Thread[Thread-0,5,main] / Value: 1
Running dec thread: Thread[Thread-1,10,main] / Value: 0
Running inc thread: Thread[Thread-0,5,main] / Value: 1
Running dec thread: Thread[Thread-1,10,main] / Value: 0
Running inc thread: Thread[Thread-0,5,main] / Value: 1
Running dec thread: Thread[Thread-1,10,main] / Value: 0
Running inc thread: Thread[Thread-0,5,main] / Value: 1
Running dec thread: Thread[Thread-1,10,main] / Value: 0
cs205: engineering software
6
Priorities
• In general, threads with higher
priorities will be scheduled
preferentially.
• There are no guarantees: up to Java
scheduler
Thread class:
void setPriority (int newPriority)
// MODIFIES: this
// EFFECTS: Changes the priority of this
//
thread to newPriority.
cs205: engineering software
7
Priorities, Priorities
ithread.setPriority (Thread.NORM_PRIORITY);
ithread.start ();
dthread.setPriority (Thread.MIN_PRIORITY);
dthread.start ();
The ithread should run more than the
dthread, but there is no guarantee.
Thread.MIN_PRIORITY
Thread.NORM_PRIORITY
Thread.MAX_PRIORITY
cs205: engineering software
8
Questions from Monday’s Notes
• Is it possible for a thread to
simultaneously be in the wait set for
more than one object?
cs205: engineering software
9
Bad Waiting
synchronized (first) {
if (first.isHeld ()) {
try { first.wait();
} catch (InterruptedException e) {
return; }
}
first.take(this);
}
No guarantee !first.isHeld() here
cs205: engineering software
10
Stopping Threads
public class java.lang.Thread {
public final void stop()
Deprecated. This method is
inherently unsafe.
Forces the thread to stop executing.
…The thread represented by this thread
is forced to stop whatever it is doing
abnormally and to throw a newly created
ThreadDeath object as an exception. …
cs205: engineering software
11
Why deprecate stop?
• What should happen to all the locks
a thread owns when it is stopped?
• What if an invariant is temporarily
broken in a method?
cs205: engineering software
12
Suspending Threads
public final void suspend()
Suspends this thread. If the thread is alive, it is
suspended and makes no further progress
unless and until it is resumed.
Deprecated. This method has been deprecated, as it is
inherently deadlock-prone. If the target thread holds a lock
on the monitor protecting a critical system resource when it
is suspended, no thread can access this resource until the
target thread is resumed. If the thread that would resume the
target thread attempts to lock this monitor prior to calling
resume, deadlock results. Such deadlocks typically manifest
themselves as "frozen" processes.
cs205: engineering software
13
Can’t stop, can’t suspend, what
can you do?
public void interrupt()
Interrupts this thread.
If this thread is blocked in an invocation of the wait(),
wait(long), or wait(long, int) methods of the Object
class, or of the join(), join(long), join(long, int),
sleep(long), or sleep(long, int), methods of this class,
then its interrupt status will be cleared and it will
receive an InterruptedException.
…
If none of the previous conditions hold then this
thread's interrupt status will be set.
cs205: engineering software
14
Being Interrupted
public boolean isInterrupted()
MODIFIES: nothing
EFFECTS: Returns true iff this
thread
has been interrupted.
cs205: engineering software
15
Counter c = new Counter ();
IncThread ithread = new IncThread (c);
DecThread dthread = new DecThread (c);
ithread.setPriority (Thread.NORM_PRIORITY);
ithread.start ();
dthread.setPriority (Thread.MAX_PRIORITY);
dthread.start ();
dthread.interrupt ();
Interrupts are just
“polite” requests!
The thread can
ignore it and keep
going…
cs205: engineering software
Running inc thread: Thread[Thread-0,5,main] / Value: 1
Running dec thread: Thread[Thread-1,10,main] / Value: 0
Running inc thread: Thread[Thread-0,5,main] / Value: 1
Running dec thread: Thread[Thread-1,10,main] / Value: 0
Running inc thread: Thread[Thread-0,5,main] / Value: 1
Running dec thread: Thread[Thread-1,10,main] / Value: 0
Running inc thread: Thread[Thread-0,5,main] / Value: 1
Running dec thread: Thread[Thread-1,10,main] / Value: 0
16
JavaSpaces
cs205: engineering software
17
Concurrency Problems
• What is the source of all concurrency
problems?
Mutability!
If nothing is mutable, then the order of
events doesn’t matter.
If we can get rid of mutation, then
concurrency should be easy!
cs205: engineering software
18
Linda
• David Gelernter (Yale, 1980s)
• Basis for JavaSpaces, Sun 1999
• Jini – Java’s distributed computing
environment
– “Network plug and play”
cs205: engineering software
19
Basic Idea
• Have a shared space (“tuple space”)
– Processes can write, read, and take
away values from this space
• Bag of processes, each looks for
work it can do by matching values in
the tuple space
• Get load balancing, synchronization,
messaging, etc. for free!
cs205: engineering software
20
JavaSpaces
cs205: engineering software
21
Tuples
Conventional
Memory
Linda/JavaSpaces
Unit
Bit
Logical Tuple
(23, “test”, false)
Access Using
Address
(variable)
Selection of values
Operations
read, write
(mutates)
take, write
immutable
cs205: engineering software
22
Tuple Space Operations
• write (t) – add tuple t to tuple space
• take (s)  t – returns and removes
tuple t matching template s
• read (s)  t – same as take, except
doesn’t remove t.
Operations are atomic (even if space
is distributed)
cs205: engineering software
23
JavaSpaces
(Not included in Java API, need to
download)
interface net.jini.space.JavaSpace {
public Entry take (Entry tmpl, Transaction txn,
long timeout)
// EFFECTS: Takes an entry in the space
// that matches the template, blocking
// until one exists. Returns null if timeout
// expires first.
cs205: engineering software
24
Entry Matching
• An entry in the Space matches a template
object if:
– Its type is a subtype of the template object
– For every field in the template object:
• The value is null: matches anything
• The value is non-null: values are identical (same bits
in representation, not equals!)
• For our examples, we’ll use Linda model:
– Matching types and literals
cs205: engineering software
25
Meaning of take
take (“f”, int n)
take (“f”, 23)
take (“t”, bool b, int n)
take (string s, int n)
Tuple Space
(“f”, 23)
(“t”, 25)
(“t”, true)
(“t”, false)
(“f”, 17)
take (“cookie”)
cs205: engineering software
26
Distributed Ebay
• Offer Item (String item, int minbid, int time):
write (item, minbid, “owner”);
sleep (time);
take (item, formal bid, formal bidder);
if (bidder  “owner”) SOLD!
• Bid (String bidder, String item, int bid):
take (item, formal highbid, formal highbidder);
if (bid > highbid) write (item, bid, bidder)
else write (item, highbid, highbidder)
How well would this work?
cs205: engineering software
27
Factorial
Setup:
for (int i = 1; i <= n; i++) write (i);
repeat n – 1 times: start FactTask
FactTask:
take (int i); take (int j); write (i * j);
What if last two elements are taken concurrently?
Eventually, tuple space contains one
entry which is the answer.
Better way to order Setup?
cs205: engineering software
28
Finishing Factorial
Setup:
for (int i = 1; i <= n; i++) write (i);
write (“workleft”, n - 1);
take (“workleft”, 0);
take (result);
FactTask:
take (“workleft”, formal w);
if (w > 0)
take (int i); take (int j); write (i * j);
write (“workleft”, w – 1);
Opps – we’ve sequentialized it!
cs205: engineering software
29
Concurrent Finishing Factorial
Setup:
repeat n-1 times: start FactWorker
out (“done”, 0);
for (int i = 1; i <= n; i++) {
write (i); if i > 1 write (“work”); }
take (“done”, n-1);
take (result);
FactWorker:
take (“work”);
take (formal int i); take (formal int j); write (i * j);
take (“done”, formal int n); write (“done”, n + 1);
cs205: engineering software
30
Sorting in Linda
• Problem: Sort an array of n integers
• Initial tuple state
(“A”, [A[0], ..., A[n-1]])
• Final tuple state:
(“A”, [A’[0], ..., A’[n-1]])
such A’ has a corresponding element for
every element in A, and
for all 0 <= j < k <= n-1, A’[j] <= A’[k]
cs205: engineering software
31
Charge
• Computers are single-threaded
machines that provide their owner
the illusion of multiple threads.
• Brains are multi-threaded machines
that provide their owner with the
illusion of a single thread.
Thread work = new Thread (ps5);
work.setPriority (Thread.MAX_PRIORITY);
work.start ();
cs205: engineering software
32