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?

