Docsity

Thread Synchronization: notify() vs. notifyAll(), InterruptedException, and Queues, Exams of Programming Languages

Toggle sidebar
bg1
1
Programming with Threads
notify() vs. notifyAll()
• Very tricky to use notify() correctly
– notifyAll() much safer
• Need:
– All waiters are equal
– Each notify only needs to wake up 1 thread
– handle InterruptedException correctly
InterruptedException
• 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
Handling InterruptedException
• synchronized (this) {
while (!ready) {
try { wait(); }
catch (InterruptedException e) {
notify();
throw e;
}
// do whatever
}
Lock/Monitor Granularity
• How many locks?
– a single lock simplifies design
– but creates a bottleneck
• How long do you hold them?
– Holding a lock blocks others threads
– Releasing and reacquiring lock
• may introduce more overhead
• give wrong semantics (transaction isn’t atomic)
Concurrent Queue
• Need to prohibit concurrent enqueue’s
• Need to prohibit concurrent dequeue’s
• Can we allow a concurrent enqueue and
dequeue?
– Yes, most of the time
pf3
pf4
pf5

Partial preview of the text

Download Thread Synchronization: notify() vs. notifyAll(), InterruptedException, and Queues and more Exams Programming Languages in PDF only on Docsity!

Programming with Threads

notify() vs. notifyAll()

  • Very tricky to use notify() correctly
    • notifyAll() much safer
  • Need:
    • All waiters are equal
    • Each notify only needs to wake up 1 thread
    • handle InterruptedException correctly

InterruptedException

  • 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

Handling InterruptedException

  • synchronized (this) { while (!ready) { try { wait(); } catch (InterruptedException e) { notify(); throw e; } // do whatever }

Lock/Monitor Granularity

  • How many locks?
    • a single lock simplifies design
    • but creates a bottleneck
  • How long do you hold them?
    • Holding a lock blocks others threads
    • Releasing and reacquiring lock
      • may introduce more overhead
      • give wrong semantics (transaction isn’t atomic)

Concurrent Queue

  • Need to prohibit concurrent enqueue’s
  • Need to prohibit concurrent dequeue’s
  • Can we allow a concurrent enqueue and

dequeue?

  • Yes, most of the time

LinkedQueue [Lea 99]

LinkedQueue

head

last

object

next

object

next

object

next

(unused)

When queue is empty

LinkedQueue

head last

object

next

(unused)

LinkedQueue

class LinkedQueue { protected Node head = new Node(null); protected Node last = head;

static class Node { Object object; Node next = null; Node (Object x) { object = x; } }

Enqueue

synchronized void enqueue(Object x) {

Node n = new Node(x);

last.next = n;

last = n;

notifyAll();

Try to Dequeue

synchronized Object tryToDequeue() { Object x = null; Node first = head.next; if (first != null) { x = first.object; first.object = null; head = first; } return x; }

Dequeue

synchronized Object dequeue() throws InterruptedException { while (head.next == null) wait(); Node first = head.next; Object x = first.object; first.object = null; head = first; return x; }

Even more parallelism

  • Say you had many concurrent enqueue and

dequeue threads

  • How to handle?
  • Assume you are willing to give up strict

first-in, first-out ordering…

Create multiple queues

  • Each thread either picks a queue at random
  • Or has a default queue, and goes to other

queues if nothing available

When should you worry about

blocking?

  • On a single processor system, blocking is

essentially never an issue,

  • so long as you don’t hold any locks while you perform any operations that are - computationally expensive, or - potentially blocking (e.g., I/O)

Custom locks

  • Can design custom locks
    • special features, such as trying for a lock or read locks
  • Built using standard locks
  • ReadWriteLock example
    • doesn’t handle recursive locks

Read/Write locks

class ReadWriteLock {

int readLocks = 0;

boolean writeLocked = false;

synchronized acquireReadLock()

throws InterruptedException {

while (writeLocked) wait();

readLocks++;

Read/Write locks

synchronized void releaseReadLock() {

readLocks--;

notifyAll();

Read/Write locks

synchronized void acquireWriteLock() throws InterruptedException { while (writeLocked || readLocks > 0) wait(); writeLocked = true; }

synchronized void releaseWriteLock() { writeLocked = false; notifyAll(); }