![Page 1: COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?](https://reader031.vdocument.in/reader031/viewer/2022013004/56649cf45503460f949c31e7/html5/thumbnails/1.jpg)
COS 461Fall 1997
Concurrent Programming
Traditional programs do one thing at a time. Concurrent programs do several things at once. Why do this?
– exploit parallel hardware– defer work (“to-do list”)– interface to slow device (disk, printer, net)– interface with person– handle many network clients at once
![Page 2: COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?](https://reader031.vdocument.in/reader031/viewer/2022013004/56649cf45503460f949c31e7/html5/thumbnails/2.jpg)
COS 461Fall 1997
Expressing Concurrency
Use a thread for each concurrent activity. Multiple threads can run within a single
program.– appear to run at the same time– really, they take turns running– scheduling happens automatically
A sequential program is a program with one thread in it.
![Page 3: COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?](https://reader031.vdocument.in/reader031/viewer/2022013004/56649cf45503460f949c31e7/html5/thumbnails/3.jpg)
COS 461Fall 1997
Starting a Thread: Method 1
class MyThread extends java.lang.Thread {…public void run() {
// code you want the thread to execute}
}
// start a threadMyThread t = new MyThread(args);t.start();
![Page 4: COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?](https://reader031.vdocument.in/reader031/viewer/2022013004/56649cf45503460f949c31e7/html5/thumbnails/4.jpg)
COS 461Fall 1997
Starting a Thread: Method 2
class MyClass implements java.lang.Runnable {…public void run() {
// code you want the thread to execute}
}
// start a threadMyClass m = new MyClass(args)Thread t = new Thread(m);t.start();
![Page 5: COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?](https://reader031.vdocument.in/reader031/viewer/2022013004/56649cf45503460f949c31e7/html5/thumbnails/5.jpg)
COS 461Fall 1997
Other Thread Operations
Thread.currentThread gets the identity of the currently-running thread.
Thread.sleep(millis) puts the calling thread to sleep for millis milliseconds.
t.stop() kills thread t. thread suicide:
Thread.currentThread.stop();– or return from run
![Page 6: COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?](https://reader031.vdocument.in/reader031/viewer/2022013004/56649cf45503460f949c31e7/html5/thumbnails/6.jpg)
COS 461Fall 1997
Shared Memory
Threads in the same program share memory.– Good: can communicate quickly and easily– bad: can stomp each other’s data structures
» horrible bugs!
Threads in a program share statics and newed up objects.
Private versions of arguments and locals.
![Page 7: COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?](https://reader031.vdocument.in/reader031/viewer/2022013004/56649cf45503460f949c31e7/html5/thumbnails/7.jpg)
COS 461Fall 1997
Deferring Work with Threadsclass DeferredGradeReport extends Thread {
private String student;
public DeferredGradeReport(String who) {student = who;start();
}
public void run() {int grade = calculateGrade(student);sendGradeToStudent(student, grade);
}
![Page 8: COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?](https://reader031.vdocument.in/reader031/viewer/2022013004/56649cf45503460f949c31e7/html5/thumbnails/8.jpg)
COS 461Fall 1997
Why are Threads Tricky?
Single thread: things change only because the program changes them
multi-threaded: things can change “on their own”
x = y;
if(x != y){// die horribly
}
x = 17;
![Page 9: COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?](https://reader031.vdocument.in/reader031/viewer/2022013004/56649cf45503460f949c31e7/html5/thumbnails/9.jpg)
COS 461Fall 1997
Another Thread-Related Disaster
class C {private int x = 3;
public void increment() {int r = x;++r;x = r;
}}
r = x;
++r;
x = r;
r = x;
++r;
x = r;
thread A thread B
3
3
4
4
4
4
![Page 10: COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?](https://reader031.vdocument.in/reader031/viewer/2022013004/56649cf45503460f949c31e7/html5/thumbnails/10.jpg)
COS 461Fall 1997
Mutual Exclusion
protect data from “outside meddling” use Java’s synchronized keyword
class C {private int x = 3;
public synchronized void increment() {int r = x;++r;x = r;
}}
![Page 11: COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?](https://reader031.vdocument.in/reader031/viewer/2022013004/56649cf45503460f949c31e7/html5/thumbnails/11.jpg)
COS 461Fall 1997
Synchronized: Details synchronized method
holds a “lock” on the object that the method is invoked on
if lock is unavailable, you’re put to sleep until you can get it
lock doesn’t prevent access to data, it only prevents locking
return
call
return
call
SLEEP
![Page 12: COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?](https://reader031.vdocument.in/reader031/viewer/2022013004/56649cf45503460f949c31e7/html5/thumbnails/12.jpg)
COS 461Fall 1997
Synchronized: Details
same thread can acquire the same lock multiple times– recursive synchronized methods “work”
also, synchronized statement in Java
C c = new C();…synchronized(c){
++(c.x);}
![Page 13: COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?](https://reader031.vdocument.in/reader031/viewer/2022013004/56649cf45503460f949c31e7/html5/thumbnails/13.jpg)
COS 461Fall 1997
Deadlock
A waiting for B; B waiting for A
we’re stuck, forever
class LLitem { private LLitem next, prev;
synchronized void remove(){ synchronized(next){ next->prev = prev; } synchronized(prev){ prev->next = next; } }}
![Page 14: COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?](https://reader031.vdocument.in/reader031/viewer/2022013004/56649cf45503460f949c31e7/html5/thumbnails/14.jpg)
COS 461Fall 1997
Avoiding Deadlock
key idea: avoid cycles of waiting can do use special-case design, then
convince yourself it’s correct usually, follow two simple rules
– never lock two objects of the same class at the same time
– if class C was written before class D, code in C never locks a D object (not even indirectly)
![Page 15: COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?](https://reader031.vdocument.in/reader031/viewer/2022013004/56649cf45503460f949c31e7/html5/thumbnails/15.jpg)
COS 461Fall 1997
Example: Blocking Queue (1.0)
public class BlockingQueue extends Queue {public synchronized void put(Object o) {
super.put(o);}
public synchronized Object get() {if(super.empty()) return null;else return super.get();
}}
![Page 16: COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?](https://reader031.vdocument.in/reader031/viewer/2022013004/56649cf45503460f949c31e7/html5/thumbnails/16.jpg)
COS 461Fall 1997
Using BlockingQueue 1.0
// get from BlockingQueue bqObject o;do{
o = bq.get();}while(o == null);
Problem: waste CPU time calling get() over and over
Solution: when queue is empty, get() shouldblock the calling thread
![Page 17: COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?](https://reader031.vdocument.in/reader031/viewer/2022013004/56649cf45503460f949c31e7/html5/thumbnails/17.jpg)
COS 461Fall 1997
BlockingQueue 2.0
public class BlockingQueue extends Queue {public synchronized void put(Object o) {
super.put(o);}
public synchronized Object get() {while(super.empty()) Thread.yield();return super.get();
}}
Deadlock!
![Page 18: COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?](https://reader031.vdocument.in/reader031/viewer/2022013004/56649cf45503460f949c31e7/html5/thumbnails/18.jpg)
COS 461Fall 1997
BlockingQueue 3.0
public class BlockingQueue extends Queue {// put omitted
public /*synchronized*/ Object get() {while(super.empty()) Thread.yield();synchronized(this) {
return super.get();}
}}
Sometimes returns null
![Page 19: COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?](https://reader031.vdocument.in/reader031/viewer/2022013004/56649cf45503460f949c31e7/html5/thumbnails/19.jpg)
COS 461Fall 1997
BlockingQueue 4.0
public class BlockingQueue extends Queue { public Object get() { Object ret; do{ while(super.empty()) Thread.yield(); synchronized(this) { ret = super.get(); } }while(ret == null); return ret; }}
Works, but inefficient
![Page 20: COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?](https://reader031.vdocument.in/reader031/viewer/2022013004/56649cf45503460f949c31e7/html5/thumbnails/20.jpg)
COS 461Fall 1997
Solution: wait/notify
idea: let a thread wait while holding a lock– temporarily gives up lock while sleeping– awakened when another thread causes
something to change Java syntax
– wait(): sleeps temporarily (giving up lock)– notify(): wakes up one sleeper – notifyAll() wakes up all sleepers
![Page 21: COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?](https://reader031.vdocument.in/reader031/viewer/2022013004/56649cf45503460f949c31e7/html5/thumbnails/21.jpg)
COS 461Fall 1997
BlockingQueue: Solution
public class BlockingQueue extends Queue {public synchronized void put(Object o) {
super.put(o);notify();
}
public synchronized Object get() {while(super.empty()) wait();return super.get();
}}
![Page 22: COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?](https://reader031.vdocument.in/reader031/viewer/2022013004/56649cf45503460f949c31e7/html5/thumbnails/22.jpg)
COS 461Fall 1997
Wait/Notify Details
several threads can wait simultaneously wait() releases lock, waits for wakeup, then
reacquires lock before continuing wake-up not immediate, so condition might not be
true when thread returns from wait()– check for condition in while-loop
OK to wake up too many threads; deadly to wake up too few– when in doubt, notifyAll()