Sync 2 processes

Hi, I am doing some tests to see if I can fully utilise the processors on a 2 core machine.

I have an algorithm that may be run in parallel on two processors. After each iteration, data from the two processes is synchronized.

I have created 3 monitors, MonitorA and MonitorB which carry out half the work of the algorithm each, and an update monitor to synchronize the iterations of MonitorA and MonitorB.

publicclass UpdateMonitor{

privateboolean aUpdated =true;

privateboolean bUpdated =true;

privateboolean aThinned =false;

privateboolean bThinned =false;

private MonitorA a;

private MonitorB b;

publicsynchronizedvoid update(){

System.out.println("call Update");

while(!aThinned||!bThinned)try{wait();}

catch(InterruptedException e){}

System.out.println("updated");

aUpdated =true;

bUpdated =true;

aThinned =false;

bThinned =false;

a.updated();

b.updated();

}

publicsynchronizedvoid setA(){

System.out.println("call setA");

aThinned =true;

aUpdated =false;

notifyAll();

}

publicsynchronizedvoid setB(){

System.out.println("call setB");

bThinned =true;

bUpdated =false;

notifyAll();

}

publicsynchronizedboolean getAUpdated(){

return aUpdated;

}

publicsynchronizedboolean getBUpdated(){

return bUpdated;

}

publicvoid registerA(MonitorA a){

this.a = a;

}

publicvoid registerB(MonitorB b){

this.b = b;

}

}

publicclass MonitorA{

UpdateMonitor psm;

public MonitorA(UpdateMonitor psm){

this.psm = psm;

}

publicsynchronizedvoid thinA(){

System.out.println("call A");

while(!psm.getAUpdated())try{ wait();}

catch(InterruptedException e){}

for(long i=0; i<100000000; i++){

}

System.out.println("thinned A");

psm.setA();

}

publicsynchronizedvoid updated(){

System.out.println("updated notify A");

notifyAll();

}

}

MonitorB is similar to A.

The problem is that the statements a.updated();b.updated();

are not able to run. I'm not sure, but I think this is because the object lock is not being released.

Can you give me some advice in how to rectify this problem?

[5541 byte] By [dalyma] at [2007-11-26 18:35:54]
# 1

You haven't shown where you create threads to execute the code, so there is no way to tell what the intended control flow is.

However you have a deadlock potential with your locking:

- UpdateMonitor.update is synchronized and calls MonitorA.updated, which is also synchronized. So this will attempt to lock psm then a

- MonitorA.thinA is synchronized and calls UpdateMonitor.getAUpdated, which is also synchronized. So this will attempt to lock a then psm

Grabbing multiple locks in a different order can lead to deadlock.

You should do some reading on locking strategies and deadlock avoidance. A question to ask with your code is whether UpdateMonitor.update really needs to execute atomically with respect to a.updated() and b.updated(). If it doesn't then you can narrow the scope of your locking to avoid the need for multiple locks from update;

public void update() {

synchronized(this) {

System.out.println("call Update");

while(!aThinned||!bThinned) try {wait(); }

catch(InterruptedException e) {}

System.out.println("updated");

aUpdated = true;

bUpdated = true;

aThinned = false;

bThinned = false;

}

a.updated();

b.updated();

}

davidholmesa at 2007-7-9 6:09:58 > top of Java-index,Core,Core APIs...
# 2
Thats a great reply, thank you for the explaination and suggestions.Michael
dalyma at 2007-7-9 6:09:58 > top of Java-index,Core,Core APIs...