java - Synchronize Protection Not Working for Dual Threads -


below stripped down java code review. have several subclasses, , when execparallel() called, new thread launched. thread , default thread must both execute criticalfunction() several times via actionfunction(), function work given subclassc connection if executed 1 process @ time.

i have used keyword "synchronized" guard against concurrent executions, in practice criticalfunction() in fact being called both threads @ same time.

any idea 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() {             (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);     } } 

if goal protect otherclass.executecritical(con, arg) invocation, you'll want lock @ granularity of otherclass instance. if goal have 1 thread using "master connection" @ given time, seems want, need locking granularity @ instance of tcpmasterconnection. in latter case, code this:

    public object criticalfunction(int arg) {         synchronized(con) {           return otherclass.executecritical(con, arg);         }     } 

now if have multithread-unsafe code in both otherclass , con (of tcpmasterconnection), you'll maybe want lock larger granularity. in case, easy thing might lock @ class level described in other answers.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -