
















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!

















Thread 1
l.lock(); m.lock(); ... m.unlock(); l.unlock();
Lock l = new ReentrantLock(); Lock m = new ReentrantLock(); Thread 2
m.lock(); l.lock(); ... l.unlock(); m.unlock();
static Lock l = new ReentrantLock();
void f () throws Exception { l.lock(); try { FileInputStream f = new FileInputStream("file.txt"); // Do something with f f.close(); } finally { // This code executed no matter how we // exit the try block l.unlock(); } }
class C { int cnt;
void inc() { synchronized (this) { cnt++; } } }
Thread 1 c.inc();
Thread 2 c.inc();
C c = new C();
class C { int cnt;
void inc() { synchronized (this) { cnt++; } } }
Thread 1 c1.inc();
Thread 2 c2.inc();
C c1 = new C(); C c2 = new C();
class C { int cnt;
void inc() { synchronized (this) { cnt++; } } }
class C { int cnt;
synchronized void inc(){ cnt++; } }
class C { static int cnt;
void inc() { synchronized (this) { cnt++; } }
static synchronized void dec() { cnt--; } }
Thread 1 c.inc();
Thread 2 C.dec();
C c = new C();