L
lonelyplanet999
Hi,
I'm studying 310-035 exam and I use "Java 2 - Sun Certified Programmer
& Developer for Java 2 (310-035) by Kathy Sierra, Bert Bates" as my
study guide.
In chapter 9 of the book there is segment of code discussing about
deadlock:
class DeadlockRisk {
private static class Resource {
public int value;
}
private Resource resourceA = new Resource();
private Resource resourceB = new Resource();
public int read() {
synchronized(resourceA) {
synchronized(resourceB) {
return resourceB.value + resourceA.value;
}
}
}
public void write(int a, int b) {
synchronized(resourceB) {
synchronized(resourceA) {
resourceA.value = a;
resourceB.value = b;
}
}
}
}
I intend to implement main() method as below:
public class P526 extends Thread {
DeadlockRisk dl;
public void register(DeadlockRisk d) {
this.dl = d;
}
public void run() {
}
public static void main(String[] args) {
DeadlockRisk dlock = new DeadlockRisk();
P526 a = new P526();
P526 b = new P526();
a.dl = dlock;
b.dl = dlock;
a.start();
b.start();
}
}
However, there is only one run() method to override, I don't know how
to control the 2 threads a & b such that one of them start calling
read() method while the other start calling write() method first.
Could anyone suggest a feasible solution for this problem ?
Tks
I'm studying 310-035 exam and I use "Java 2 - Sun Certified Programmer
& Developer for Java 2 (310-035) by Kathy Sierra, Bert Bates" as my
study guide.
In chapter 9 of the book there is segment of code discussing about
deadlock:
class DeadlockRisk {
private static class Resource {
public int value;
}
private Resource resourceA = new Resource();
private Resource resourceB = new Resource();
public int read() {
synchronized(resourceA) {
synchronized(resourceB) {
return resourceB.value + resourceA.value;
}
}
}
public void write(int a, int b) {
synchronized(resourceB) {
synchronized(resourceA) {
resourceA.value = a;
resourceB.value = b;
}
}
}
}
I intend to implement main() method as below:
public class P526 extends Thread {
DeadlockRisk dl;
public void register(DeadlockRisk d) {
this.dl = d;
}
public void run() {
}
public static void main(String[] args) {
DeadlockRisk dlock = new DeadlockRisk();
P526 a = new P526();
P526 b = new P526();
a.dl = dlock;
b.dl = dlock;
a.start();
b.start();
}
}
However, there is only one run() method to override, I don't know how
to control the 2 threads a & b such that one of them start calling
read() method while the other start calling write() method first.
Could anyone suggest a feasible solution for this problem ?
Tks