Download presentation
Presentation is loading. Please wait.
1
Concurrency without Locks
cs205: engineering software university of virginia fall 2006 Concurrency without Locks Kramer Sharp
2
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)
3
wait and notify waiting awake, but not running Thread A Thread B
synchronized (o) o.wait () synchronized (o) { waiting o.notify () awake, but not running } // end synchronized can reclaim o lock
4
wait and notify waiting waiting still waiting If multiple
Thread C Thread A synchronized (o) Thread B If multiple threads are waiting on the same object, any one of them can be awakened o.wait () synchronized (o) waiting o.wait () synchronized (o) { waiting o.notify () awake, not running still waiting } // end synchronized
5
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 { … while (c.getValue () <= 0) { try { c.wait (); } catch (InterruptedException e) { ; } } c.decrement (); System.err.println ("Running dec thread: " + …);
6
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
7
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.
8
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
9
Questions from Monday’s Notes
Is it possible for a thread to simultaneously be in the wait set for more than one object?
10
Bad Waiting synchronized (first) { if (first.isHeld ()) {
try { first.wait(); } catch (InterruptedException e) { return; } } first.take(this); No guarantee !first.isHeld() here
11
Stopping Threads Deprecated. This method is inherently unsafe.
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. …
12
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?
13
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.
14
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.
15
Being Interrupted public boolean isInterrupted() MODIFIES: nothing
EFFECTS: Returns true iff this thread has been interrupted.
16
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 (); Running inc thread: Thread[Thread-0,5,main] / Value: 1 Running dec thread: Thread[Thread-1,10,main] / Value: 0 Interrupts are just “polite” requests! The thread can ignore it and keep going…
17
JavaSpaces
18
If we can get rid of mutation, then concurrency should be easy!
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!
19
Linda David Gelernter (Yale, 1980s) Basis for JavaSpaces, Sun 1999
Jini – Java’s distributed computing environment “Network plug and play”
20
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!
21
JavaSpaces
22
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
23
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)
24
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.
25
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
26
Meaning of take take (“f”, int n) take (“f”, 23)
take (“t”, bool b, int n) take (string s, int n) take (“cookie”) Tuple Space (“f”, 23) (“t”, 25) (“t”, true) (“t”, false) (“f”, 17)
27
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?
28
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); Eventually, tuple space contains one entry which is the answer. What if last two elements are taken concurrently? Better way to order Setup?
29
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
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);
31
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]
32
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 ();
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.