Slides on Threads and Synchronization - Fall 2004 | CMSC 433, Study Guides, Projects, Research of Programming Languages

Material Type: Project; Class: PROG LANG TECH & PDGMS; Subject: Computer Science; University: University of Maryland; Term: Spring 2004;

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 07/30/2009

koofers-user-la5
koofers-user-la5 🇺🇸

9 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2004
Threads and Synchronization
April 1, 2004
(thanks to Doug Lea for some slides)
54
Administrivia
Project 4 posted
Don’t forget to submit exercises file
I.e., don’t just submit *.java
Remember to watch newsgroup, web page
Updates on web page
Some hints on project 4 on the newsgroup
Midterm “grades” mailed out in a few days
After project 3 regrades are done
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Slides on Threads and Synchronization - Fall 2004 | CMSC 433 and more Study Guides, Projects, Research Programming Languages in PDF only on Docsity!

CMSC 433 – Programming Language

Technologies and Paradigms

Spring 2004

Threads and Synchronization

April 1, 2004

(thanks to Doug Lea for some slides) 54

Administrivia

  • Project 4 posted
    • Don’t forget to submit exercises file
    • I.e., don’t just submit *.java
  • Remember to watch newsgroup, web page
    • Updates on web page
    • Some hints on project 4 on the newsgroup
  • Midterm “grades” mailed out in a few days
    • After project 3 regrades are done

55

Avoiding Interference:

Synchronization

public class Example extends Thread { private static int cnt = 0; static Object lock = new Object(); public void run() { synchronized (lock) { int y = cnt; cnt = y + 1; } } … } Lock, for protecting the shared state Acquires the lock; Only succeeds if not held by another thread Releases the lock 56

Applying Synchronization

int cnt = 0; t1.run() { synchronized(lock) { int y = cnt; cnt = y + 1; } } t2.run() { synchronized(lock) { int y = cnt; cnt = y + 1; } } Shared state cnt = 0 T1 acquires the lock

59

Applying Synchronization

int cnt = 0; t1.run() { synchronized(lock) { int y = cnt; cnt = y + 1; } } t2.run() { synchronized(lock) { int y = cnt; cnt = y + 1; } } Shared state cnt = 1 T1 runs, assigning to cnt y = 0 60

Applying Synchronization

int cnt = 0; t1.run() { synchronized(lock) { int y = cnt; cnt = y + 1; } } t2.run() { synchronized(lock) { int y = cnt; cnt = y + 1; } } Shared state cnt = 1 T1 releases the lock and terminates y = 0

61

Applying Synchronization

int cnt = 0; t1.run() { synchronized(lock) { int y = cnt; cnt = y + 1; } } t2.run() { synchronized(lock) { int y = cnt; cnt = y + 1; } } Shared state cnt = 1 T2 now can acquire the lock. y = 0 62

Applying Synchronization

int cnt = 0; t1.run() { synchronized(lock) { int y = cnt; cnt = y + 1; } } t2.run() { synchronized(lock) { int y = cnt; cnt = y + 1; } } Shared state cnt = 1 T2 reads cnt into y. y = 0 y = 1

65

Synchronized Statement

  • synchronized (obj) { statements }
  • Obtains the lock on obj before executing

statements in block

  • Releases the lock when the statement block

completes

  • Either normally, or due to a return, break, or exception being thrown in the block 66

Synchronized Methods

  • A method can be synchronized
    • Add synchronized modifier before return type
  • Obtains the lock on object referenced by this before executing method - Releases lock when method completes
  • For a static synchronized method
    • Locks the Class object for the class
      • Accessible directly, e.g. Foo.class
    • Not the same as this!

67

Synchronization Example

public class State { private int cnt = 0; public int synchronized incCnt(int x) { cnt += x; } public int synchronized getCnt() { return cnt; } } public class MyThread extends Thread { State s; public MyThread(State s) { this.s = s; } public void run() { s.incCnt(1) } public void main(String args[]) { State s = new State(); MyThread thread1 = new MyThread(s); MyThread thread2 = new MyThread(s); thread1.start(); thread2.start(); } } Synchronization occurs in State object itself, rather than in its caller. 68

Synchronization Style

  • Design decision
    • Internal synchronization (class is thread-safe)
      • Have a stateful object synchronize itself (e.g., with synchronized methods)
    • External synchronization (class is thread-compatible)
      • Have callers perform synchronization before calling the object
  • Can go both ways:
    • Thread-safe: Random
    • Thread-compatible: ArrayList, HashMap, …

71

Deadlock: Wait graphs

A T1 Thread^ T1^ holds^ lock^ A T2 B Thread T2 attempting to acquire lock B Deadlock occurs when there is a cycle in the graph 72

Wait graph example

A T T2 B T1 holds lock on A T2 holds lock on B T1 is trying to acquire a lock on B T2 is trying to acquire a lock on A

73

Key Ideas

  • Multiple threads can run simultaneously
    • Either truly in parallel on a multiprocessor
    • Or can be scheduled on a single processor
      • A running thread can be pre-empted at any time
  • Threads can share data
    • In Java, only fields can be shared
    • Need to prevent interference
      • Synchronization is one way, but not the only way
    • Overuse use of synchronization can create deadlock
      • Violation of liveness 74

Guaranteeing Safety

  • Ensure objects are accessible only when in a

consistent and appropriate state

  • All invariants are maintained
  • Presents subclass obligations
  • Use locks to enforce this
  • Rule of thumb 1: You must hold a lock when accessing shared data
  • Rule of thumb 2: You must not release a lock until shared data is in a valid state

77

Producer/Consumer Example

public class ProducerConsumer { private boolean valueReady = false; private Object value; synchronized void produce(Object o) throws InterruptedException { while (valueReady) wait(); value = o; valueReady = true; notifyAll(); } synchronized Object consume() throws InterruptedException { while (!valueReady) wait(); valueReady = false; Object o = value; value = null; // why do we do this? notifyAll(); return o; } } 78

Wait and Notify

  • Both must be called while lock is held on a
  • a.wait()
    • Releases the lock on a
      • But not any other locks acquired by this thread
    • Adds the thread to the wait set for a
    • Blocks the thread
  • a.wait(int m)
    • Limits wait time to m milliseconds

79

Wait and Notify (cont.)

  • a.notify() resumes one thread from a’s wait

set

  • No control over which thread
  • a.notifyAll() resumes all threads on a’s

wait set

  • Resumed thread(s) must reacquire lock

before continuing

  • Java performs the reacquire automatically