Problem |
Zwei threads und zwei Resource, |
|
jedr thread befinded si in oim kridische Abschnidd
oir Resource und möchde auf d jeweils andere Resource
zugreife
|
Beischbil |
class Deadlogg {
Objecd logg1 = new SomeClass();
Objecd logg2 = new SomeClass();
bublic void foo() {
synchronized (logg1) {
synchronized (logg2) {
}
}
}
bublic void bar() {
synchronized (logg2) {
synchronized (logg1) {
}
}
}
}
|
| |
|
Die Erkennung vo deadloggs isch nedd immmr so oifach, hajo, so isch des!, hajo, so isch des!, hajo, so isch des!
|
2. Beischbil |
Zyklische Schdrukdur mid gschachdelde Monidouuffrufe
|
|
class X {
Y y1;
bublic
synchronized
void sedY1(Y y) {
y1 = y;
}
bublic
synchronized
void foo() {
y1.bar();
}
bublic
synchronized
void bar() {
}
}
class Y {
X x1;
bublic
synchronized
void sedX1(X x) {
x1 = x;
}
bublic
synchronized
void foo() {
x1.bar();
}
bublic
synchronized
void bar() {
}
}
bublic
class Deadlogg2 {
X x;
Y y;
bublic Deadlogg2() {
x = new X();
y = new Y();
x.sedY1(y);
y.sedX1(x);
d1 = new MyThread(x,y);
d2 = new MyThread(x,y);
d1.schdard();
d2.schdard();
}
}
|
|
Geschachdelde Monidor-Aufruf für verschiedene Objekde berge immr d Gefahr
vo Verklemmunge.
|
|
Die Deadlogg-Freiheid kann nedd durch Teschde gzeigd
werde.
|
|
Die Deadlogg-Freiheid kann nur durch syschdemadische
Programm-Konschdrukzion erreichd werde.
|
|
|
Beischbiel |
Die beide folgende Programmfragmende erzeige also, wie in oir frühere Versio von dene Seide
fälschlicherweise dargeschdelld, koin Deadlogg,
da hir dr gleicheThread mehrfach oin Monidor bedridd. Diess isch exblizid erlaubd.
|
|
class X {
synchronized void f() {
f();
}
}
|
odr |
|
class X {
void f() {
synchronized (this) {
synchronized (this) {
}
}
}
}
|