Transcript Slides

Lecture 17:
Concurrency
without Locks
(Today’s notes: on web only.)
CS201j: Engineering Software
University of Virginia
Computer Science
David Evans
http://www.cs.virginia.edu/evans
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. …
30 October 2003
CS 201J Fall 2003
2
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?
30 October 2003
CS 201J Fall 2003
3
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.
30 October 2003
CS 201J Fall 2003
4
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.
30 October 2003
CS 201J Fall 2003
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 ();
dthread.interrupt ();
Interrupts are just
“polite” requests!
The thread can
ignore it and keep
going…
30 October 2003
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
CS 201J Fall 2003
6
JavaSpaces
30 October 2003
CS 201J Fall 2003
7
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!
30 October 2003
CS 201J Fall 2003
8
Linda
• David Gelernter (Yale, 1980s)
• Basis for JavaSpaces, Sun 1999
• Jini – Java’s distributed computing
environment
– “Network plug and play”
30 October 2003
CS 201J Fall 2003
9
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!
30 October 2003
CS 201J Fall 2003
10
JavaSpaces
30 October 2003
CS 201J Fall 2003
11
Tuples
Conventional
Memory
Linda/JavaSpaces
Unit
Bit
Logical Tuple
(23, “test”, false)
Access Using
Address (variable)
Selection of values
Operations
read, write
(mutates)
read, write, take
immutable
30 October 2003
CS 201J Fall 2003
12
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 in, except doesn’t
remove t.
Operations are atomic (even if space is
distributed)
30 October 2003
CS 201J Fall 2003
13
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.
30 October 2003
CS 201J Fall 2003
14
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
30 October 2003
CS 201J Fall 2003
15
Meaning of take
take (“f”, int n)
Tuple Space
take (“f”, 23)
(“f”, 23)
take (“t”, bool b, int n)
(“t”, 25)
(“t”, true)
(“t”, false)
take (string s, int n)
(“f”, 17)
take (“cookie”)
30 October 2003
CS 201J Fall 2003
16
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 could a bidder cheat?
30 October 2003
CS 201J Fall 2003
17
Factorial
Setup:
for (int i = 1; i <= n; i++) write (i);
start FactTask (replicated n-1 times)
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?
30 October 2003
CS 201J Fall 2003
18
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!
30 October 2003
CS 201J Fall 2003
19
Concurrent Finishing Factorial
Setup:
start FactWorker (replicated n-1 times)
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);
30 October 2003
CS 201J Fall 2003
20
Sorting in Linda
• Problem: Sorting 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]
30 October 2003
CS 201J Fall 2003
21
Charge
• Implementing an efficient, scalable
tuple space (that provides the correct
global semantics) is hard, but makes
concurrency easy
• Section tomorrow (optional):
– Feedback on your PS5 part 2 proposals
– Get started on PS5 part 2
30 October 2003
CS 201J Fall 2003
22