Lecture 17 Concurrency without Locks Todays notes on

  • Slides: 22
Download presentation
Lecture 17: Concurrency without Locks (Today’s notes: on web only. ) CS 201 j:

Lecture 17: Concurrency without Locks (Today’s notes: on web only. ) CS 201 j: Engineering Software University Virginia 30 Octoberof 2003 Computer Science CS 201 J Fall 2003 David Evans http: //www. cs. virginia. edu/evans

Stopping Threads public class java. lang. Thread { public final void stop() Deprecated. This

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 Thread. Death object as an exception. … 30 October 2003 CS 201 J Fall 2003 2

Why deprecate stop? • What should happen to all the locks a thread owns

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 201 J Fall 2003 3

Suspending Threads public final void suspend() Suspends this thread. If the thread is alive,

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 201 J Fall 2003 4

Can’t stop, can’t suspend, what can you do? public void interrupt() Interrupts this thread.

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, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an Interrupted. Exception. … If none of the previous conditions hold then this thread's interrupt status will be set. 30 October 2003 CS 201 J Fall 2003 5

Counter c = new Counter (); Inc. Thread ithread = new Inc. Thread (c);

Counter c = new Counter (); Inc. Thread ithread = new Inc. Thread (c); Dec. Thread dthread = new Dec. Thread (c); ithread. set. Priority (Thread. NORM_PRIORITY); ithread. start (); dthread. set. Priority (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 CS 201 J Fall 2003 6

Java. Spaces 30 October 2003 CS 201 J Fall 2003 7

Java. Spaces 30 October 2003 CS 201 J Fall 2003 7

Concurrency Problems • What is the source of all concurrency problems? Mutability! If nothing

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 201 J Fall 2003 8

Linda • David Gelernter (Yale, 1980 s) • Basis for Java. Spaces, Sun 1999

Linda • David Gelernter (Yale, 1980 s) • Basis for Java. Spaces, Sun 1999 • Jini – Java’s distributed computing environment – “Network plug and play” 30 October 2003 CS 201 J Fall 2003 9

Basic Idea • Have a shared space (“tuple space”) – Processes can write, read,

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 201 J Fall 2003 10

Java. Spaces 30 October 2003 CS 201 J Fall 2003 11

Java. Spaces 30 October 2003 CS 201 J Fall 2003 11

Tuples Conventional Memory Linda/Java. Spaces Unit Bit Logical Tuple (23, “test”, false) Access Using

Tuples Conventional Memory Linda/Java. Spaces 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 201 J Fall 2003 12

Tuple Space Operations • write (t) – add tuple t to tuple space •

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 201 J Fall 2003 13

Java. Spaces Not included in Java API (need to download) interface net. jini. space.

Java. Spaces Not included in Java API (need to download) interface net. jini. space. Java. Space { 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 201 J Fall 2003 14

Entry Matching • An entry in the Space matches a template object if: –

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 201 J Fall 2003 15

Meaning of take (“f”, int n) Tuple Space take (“f”, 23) take (“t”, bool

Meaning of take (“f”, int n) Tuple Space take (“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 201 J Fall 2003 16

Distributed Ebay • Offer Item (String item, int minbid, int time): write (item, minbid,

Distributed Ebay • Offer Item (String item, int minbid, int time): write (item, minbid, “owner”); sleep (time); take (item, formal bidder); if (bidder “owner”) SOLD! • Bid (String bidder, String item, int bid): take (item, formal highbidder); if (bid > highbid) write (item, bidder) else write (item, highbidder) How could a bidder cheat? 30 October 2003 CS 201 J Fall 2003 17

Factorial Setup: for (int i = 1; i <= n; i++) write (i); start

Factorial Setup: for (int i = 1; i <= n; i++) write (i); start Fact. Task (replicated n-1 times) Fact. Task: 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 201 J Fall 2003 18

Finishing Factorial Setup: for (int i = 1; i <= n; i++) write (i);

Finishing Factorial Setup: for (int i = 1; i <= n; i++) write (i); write (“workleft”, n - 1); take (“workleft”, 0); take (result); Fact. Task: 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 201 J Fall 2003 19

Concurrent Finishing Factorial Setup: start Fact. Worker (replicated n-1 times) out (“done”, 0); for

Concurrent Finishing Factorial Setup: start Fact. Worker (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); Fact. Worker: 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 201 J Fall 2003 20

Sorting in Linda • Problem: Sorting an array of n integers • Initial tuple

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 201 J Fall 2003 21

Charge • Implementing an efficient, scalable tuple space (that provides the correct global semantics)

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 PS 5 part 2 proposals – Get started on PS 5 part 2 30 October 2003 CS 201 J Fall 2003 22