Docsity

Wait and Notify with Synchronization Examples | CMSC 433, Study Guides, Projects, Research of Programming Languages

Toggle sidebar
bg1
CMSC 433, Alan Sussman, U. Maryland (via
Bill Pugh) 1
CMSC433, Spring 2002
Programming Language
Technology and Paradigms
Java Threads
Alan Sussman
February 26, 2002
CMCS 433, Spring 2002 -Alan Sussman 2
Administrivia
•Project 2 due tomorrow at 6PM
–submit as project 2
•Project 1 commentary due March 6
–submit as project 11
–finish project 1 postmortem today
•Exam 1 coming up March 7
–practice exam posted later this weeks
•Java reading posted (finally)
CMCS 433, Spring 2002 -Alan Sussman 3
Last time -Java
•Multithreading
–Runnable interface vs. extending Thread
–InterruptedException –via interrupt()
–Synchronization
•lock per object
•synchronized methods and statements
CMCS 433, Spring 2002 -Alan Sussman 4
Synchronization example
class SyncTestextends 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, Spring 2002 -Alan Sussman 5
Wait and Notify
•Must be called inside synchronized method
or block of statements
•a.wait()
–gives up lock(s) on a
–adds thread to wait set for a
–suspends thread
•a.wait(int m)
–limits suspension to m milliseconds
CMCS 433, Spring 2002 -Alan Sussman 6
Wait and Notify (cont.)
•a.notify() resumes one thread from a’s wait list
–and removes it from wait set
–no control over which thread
•a.notifyAll() resumes all threads on a’s wait list
•resumed thread(s) must reacquire lock before
continuing
•wait doesn’t give up locks on any other objects
–e.g., acquired by methods that called this one
pf3

Partial preview of the text

Download Wait and Notify with Synchronization Examples | CMSC 433 and more Study Guides, Projects, Research Programming Languages in PDF only on Docsity!

CMSC 433, Alan Sussman, U. Maryland (via

CMSC433, Spring 2002

Programming Language

Technology and Paradigms

Java Threads

Alan Sussman

February 26, 2002

CMCS 433, Spring 2002 - Alan Sussman 2

Administrivia

• Project 2 due tomorrow at 6PM

  • submit as project 2

• Project 1 commentary due March 6

  • submit as project 11
  • finish project 1 postmortem today

• Exam 1 coming up March 7

  • practice exam posted later this weeks

• Java reading posted (finally)

CMCS 433, Spring 2002 - Alan Sussman 3

Last time - Java

• Multithreading

  • Runnable interface vs. extending Thread
  • InterruptedException – via interrupt()
  • Synchronization
    • lock per object
    • synchronized methods and statements

CMCS 433, Spring 2002 - Alan Sussman 4

Synchronization example

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, Spring 2002 - Alan Sussman 5

Wait and Notify

• Must be called inside synchronized method

or block of statements

• a.wait()

  • gives up lock(s) on a
  • adds thread to wait set for a
  • suspends thread

• a.wait(int m)

  • limits suspension to m milliseconds CMCS 433, Spring 2002 - Alan Sussman 6

Wait and Notify (cont.)

  • a.notify() resumes one thread from a ’s wait list
    • and removes it from wait set
    • no control over which thread
  • a.notifyAll() resumes all threads on a ’s wait list
  • resumed thread(s) must reacquire lock before continuing
  • wait doesn’t give up locks on any other objects
    • e.g., acquired by methods that called this one

CMSC 433, Alan Sussman, U. Maryland (via

CMCS 433, Spring 2002 - Alan Sussman 7

Producer/Consumer Example –

Too Much Synchronization

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, Spring 2002 - Alan Sussman 8

Changed example –

Attempt to refine synch.

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, Spring 2002 - Alan Sussman 9

A Better Solution

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, Spring 2002 - Alan Sussman 10

notify() vs. notifyAll()

• Very tricky to use notify() correctly

  • notifyAll() generally much safer

• To use correctly, should have:

  • all waiters are equal
  • each notify only needs to wake up 1 thread
  • handle InterruptedException correctly

CMCS 433, Spring 2002 - Alan Sussman 11

InterruptedException Example

• Threads t1 and t2 are waiting

• Thread t3 performs a notify

  • thread t1 is selected

• Before t1 can acquire lock, t1 is interrupted

• t1’s call to wait throws InterruptedException

  • t1 doesn’t process notification
  • t2 doesn’t wake up CMCS 433, Spring 2002 - Alan Sussman 12

Handling InterruptedException

synchronized (this) {

while (!ready) {

try { wait(); }

catch (InterruptedException e) {

notify();

throw e; }

// do whatever