Below is my stripped down java code for review. I have several subclasses, and when execParallel() is called, a new thread is launched. This thread and the default thread must both execute criticalFunction() several times via actionFunction(), but this function will only work properly for a given SubClassC connection if executed by only one process at a time.
I have used the keyword "synchronized" to guard against concurrent executions, however in practice the criticalFunction() is in fact being called by both threads at the same time.
Any idea What I am doing wrong?
public class MainClass extends GlobalLibrary {
public static SubClassA masterObj;
public MainClass() {
masterObj = new SubClassA();
}
public static class SubClassA {
public SubClassB subObj1;
public SubClassB subObj2;
public SubClassA() {
subObj1 = new SubClassB();
subObj2 = new SubClassB();
}
}
public static class SubClassB {
public SubClassC conObj;
public Thread ut = null;
public SubClassB() {
conObj = new SubClassC();
}
}
public static class SubClassC {
public TCPMasterConnection con=null;
public SubClassC() {
con = new TCPMasterConnection();
}
public synchronized Object criticalFunction(int arg) {
return otherClass.executeCritical(con, arg);
}
}
public boolean actionFunction(SubClassB subObj, int arg) {
return (subObj.conObj.criticalFunction(arg)==null);
}
public class ActionThread implements Runnable {
public SubClassB subObj;
private int icode;
public ActionThread(SubClassB arg1, int arg2) {
subObj = arg1;
icode = arg2;
}
public void run() {
for (int i=0; i<10; i++) actionFunction(subObj, icode);
}
}
public void execParallel() {
masterObj.subObj1.ut = new Thread(new ActionThread(masterObj.subObj1, 1));
masterObj.subObj1.ut.start();
actionFunction(masterObj.subObj1, 2);
actionFunction(masterObj.subObj1, 3);
actionFunction(masterObj.subObj1, 4);
actionFunction(masterObj.subObj1, 5);
actionFunction(masterObj.subObj1, 6);
}
}