


























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



























Key idea: maintain a
part invariant on three sets
Vertices already visited (“settled”). Distance known
Frontier nodes. One hop from the settled ones
Future nodes.
one hop from the settled ones
Instead of a single
on the chip
common. Intel has prototypes with
Argues for having a way to do many things at once
Keeping those cores busy
But there may still be extra threads...
Garbage collection runs in a “background” thread
GUIs have a separate thread that listens for events and “dispatches” upcalls
Special problems arise
They see the same data and hence can interfere with each other, e.g. if one thread is modifying a complex structure like a heap while another is trying to read it
Race conditions
Deadlock
class PrimeRun implements Runnable { long a, b; PrimeRun(long a, long b) { this.a = a; this.b = b; } public void run() { //compute primes between a and b... } } PrimeRun p = new PrimeRun(143, 195);new Thread(p).start();
Thread[Thread-0,5,main] 0 Thread[main,5,main] 0 Thread[main,5,main] 1 Thread[main,5,main] 2 Thread[main,5,main] 3 Thread[main,5,main] 4 Thread[main,5,main] 5 Thread[main,5,main] 6 Thread[main,5,main] 7 Thread[main,5,main] 8 Thread[main,5,main] 9 Thread[Thread-0,5,main] 1 Thread[Thread-0,5,main] 2 Thread[Thread-0,5,main] 3 Thread[Thread-0,5,main] 4 Thread[Thread-0,5,main] 5 Thread[Thread-0,5,main] 6 Thread[Thread-0,5,main] 7 Thread[Thread-0,5,main] 8 Thread[Thread-0,5,main] 9 public class ThreadTest extends Thread { public static void main(String[] args) { new ThreadTest().start(); for (int i = 0; i < 10; i++) { System. out .format("%s %d\n", Thread. currentThread (), i); } } public void run() { for (int i = 0; i < 10; i++) { System. out .format("%s %d\n", Thread. currentThread (), i); } } } docsity.com
Thread[main,5,main] 0 Thread[main,5,main] 1 Thread[main,5,main] 2 Thread[main,5,main] 3 Thread[main,5,main] 4 Thread[main,5,main] 5 Thread[Thread-0,6,main] 0 Thread[Thread-0,6,main] 1 Thread[Thread-0,6,main] 2 Thread[Thread-0,6,main] 3 Thread[Thread-0,6,main] 4 Thread[Thread-0,6,main] 5 Thread[Thread-0,6,main] 6 Thread[Thread-0,6,main] 7 Thread[Thread-0,6,main] 8 Thread[Thread-0,6,main] 9 Thread[main,5,main] 6 Thread[main,5,main] 7 Thread[main,5,main] 8 Thread[main,5,main] 9 public class ThreadTest extends Thread { public static void main(String[] args) { new ThreadTest().start(); for (int i = 0; i < 10; i++) { System. out .format("%s %d\n", Thread. currentThread (), i); } } public void run() { currentThread ().setPriority(6); for (int i = 0; i < 10; i++) { System. out .format("%s %d\n", Thread. currentThread (), i); } } } docsity.com
waiting...running...waiting...running...waiting...running...waiting...running...waiting...running...waiting...running...waiting...running...waiting...running...waiting...running...waiting...running...done public class ThreadTest extends Thread { static boolean ok = true; public static void main(String[] args) { new ThreadTest().start(); for (int i = 0; i < 10; i++) { System. out .println("waiting..."); yield (); } ok = false; } public void run() { while ( ok ) { System. out .println("running..."); yield (); } System. out .println("done"); } } If threads happen to be sharing a CPU, yield allows other waiting threads to run. But if there are multiple cores, yield isn’t needed
It may not actually be “running”. For that, a
must schedule it. Higher priority threads could run first.
It can call Thread.sleep(k) to sleep for k milliseconds
If it tries to do
(e.g. read a file, wait for mouse input, even open a file) this can cause it to pause
Java has a form of locks associated with objects. When threads lock an object, one succeeds at a time.
Foreground threads are the ones doing visible work, like interacting with the user or updating the display
Background threads do things like maintaining data structures (rebalancing trees, garbage collection, etc)