









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!










CMCS 433, Fall 2002 - Michael Hicks 32
CMCS 433, Fall 2002 - Michael Hicks 33
CMCS 433, Fall 2002 - Michael Hicks 34
while (true) { System.out.print("Alarm> ");
// read user input String line = b.readLine(); parseInput(line);
// wait (in secs) try { Thread.sleep(timeout * 1000); } catch (InterruptedException e) { } System.out.println("("+timeout+") "+msg); }
CMCS 433, Fall 2002 - Michael Hicks 35
public class AlarmThread extends Thread { private String msg = null; private int timeout = 0;
public AlarmThread(String msg, int time) { this.msg = msg; this.timeout = time; }
public void run() { try { Thread.sleep(timeout * 1000); } catch (InterruptedException e) { } System.out.println("("+timeout+") "+msg); } }
CMCS 433, Fall 2002 - Michael Hicks 38
public class AlarmRunnable implements Runnable { private String msg = null; private int timeout = 0;
public AlarmRunnable(String msg, int time) { this.msg = msg; this.timeout = time; }
public void run() { try { Thread.sleep(timeout * 1000); } catch (InterruptedException e) { } System.out.println("("+timeout+") "+msg); } }
CMCS 433, Fall 2002 - Michael Hicks 39
CMCS 433, Fall 2002 - Michael Hicks 40
public class ThreadDemo implements Runnable { public void run() { for (int i = 5; i > 0; i--) { System.out.println(i); try { Thread.sleep(1000); } catch(InterruptedException e) { }; } System.out.println(“exiting “ + Thread.currentThread()); } public static void main(String [] args) { Thread t = new Thread(new ThreadDemo(), ”Demo Thread”); System.out.println(“main thread: “ + Thread.currentThread()); System.out.println(“Thread created: “ + t); t.start(); try { Thread.sleep(3000); } catch (InterruptedException e) {}; System.out.println(“exiting “+Thread.currentThread()); } }
CMCS 433, Fall 2002 - Michael Hicks 41
CMCS 433, Fall 2002 - Michael Hicks 44
CMCS 433, Fall 2002 - Michael Hicks 45
while (true) { System.out.print("Alarm> ");
// read user input String line = b.readLine(); Thread t = parseInput(line);
// wait (in secs) asynchronously if (t != null) t.start(); // wait for the thread to complete t.join(); }
CMCS 433, Fall 2002 - Michael Hicks 46
CMCS 433, Fall 2002 - Michael Hicks 47
CMCS 433, Fall 2002 - Michael Hicks 50
CMCS 433, Fall 2002 - Michael Hicks 51
CMCS 433, Fall 2002 - Michael Hicks 52
CMCS 433, Fall 2002 - Michael Hicks 53
class SyncTest extends Thread { String msg; public SyncTest(String s) { msg = s; start(); } public void run() { synchronized (SyncTest.class) { System.out.print(“[“ + msg); try { Thread.sleep(1000); } catch (InterruptedException e) {}; System.out.println(“]”); } } public static void main(String [] args) { new SyncTest(“Hello”); new SyncTest(“Synchronized”); new SyncTest(“World”); } }
CMCS 433, Fall 2002 - Michael Hicks 56
public class ProducerConsumer { private boolean ready = false; private Object obj; public ProducerConsumer() { } public ProducerConsumer(Object o) { obj = o; ready = true; }
synchronized void produce(Object o) { while (ready) wait(); obj = o; ready = true; notifyAll(); }
synchronized Object consume() { while (!ready) wait(); ready = false; notifyAll(); return obj; } }
CMCS 433, Fall 2002 - Michael Hicks 57
synchronized void produce(Object o) { while (ready) { wait(); if (ready) notify(); } obj = o; ready = true; notify(); }
synchronized Object consume() { while (!ready) { wait(); if (!ready) notify(); } ready = false; notify(); return obj; }
Doesn’t work well – no guarantee about who will get woken up
CMCS 433, Fall 2002 - Michael Hicks 58
synchronized void produce(Object o) { while (ready) { synchronized (empty) { try {empty.wait(); } catch (InterruptedException e) { } }} obj = o; ready = true; synchronized (full) { full.notify(); } }
synchronized Object consume() { while (!ready) { synchronized (full) { try { full.wait(); } catch (InterruptedException e) { }} Object o = obj; ready = false; synchronized(empty){ empty.notify(); } return obj; }
Use two objects, empty and full , to allow two different wait sets
CMCS 433, Fall 2002 - Michael Hicks 59