








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Project; Class: PROG LANG TECH & PDGMS; Subject: Computer Science; University: University of Maryland; Term: Spring 2004;
Typology: Study Guides, Projects, Research
1 / 14
This page cannot be seen from the preview
Don't miss anything!









(thanks to Doug Lea for some slides) 54
55
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
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
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
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
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
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
67
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
71
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
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
77
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
79