java - Lock object which fields are actually being read -


we have such situation

    class pole extends thread {     jbutton pole;     plansza p;     pole neighbours[] = new pole[4];     public pole(plansza p)     {         this.p = p;          pole = new jbutton();         int r,g,b;         r=p.rndcolor();         g=p.rndcolor();         b=p.rndcolor();         pole.setbackground(new color(r,g,b));     }     public pole()     {         ;     }      public void run()     {         while(true)         {             thread.yield();             try             {                 thread.sleep((int)p.rndtime());             }             catch(interruptedexception e)             {                 ;             }             if(p.rnd.nextdouble()<=1-p.p)                 setneighbourscolor();             if(p.rnd.nextdouble()<=p.p)                 setrandomcolor();         }     }      public void setrandomcolor()     {         synchronized(this)         {             int r,g,b;             r = p.rndcolor();             g = p.rndcolor();             b = p.rndcolor();             pole.setbackground(new color(r,g,b));         }     }     public void setneighbourscolor()     {         synchronized(this)         {              int r,g,b;             color c0 = neighbours[0].pole.getbackground();             color c1 = neighbours[1].pole.getbackground();             color c2 = neighbours[2].pole.getbackground();             color c3 = neighbours[3].pole.getbackground();             r = (int)(c0.getred() + c1.getred() + c2.getred() + c3.getred())/4;             g = (int)(c0.getgreen() + c1.getgreen() + c2.getgreen() + c3.getgreen())/4;             b = (int)(c0.getblue() + c1.getblue() + c2.getblue() + c3.getblue())/4;             color nc = new color(r,g,b);             pole.setbackground(nc);         }     } } 

i'v edited question , pasted code, maybe little bit clearer each pole(field) has in neighbour relation 4 other pole objects have many such objects , each different thread, can read colour neighbours , change self colour arithmetic average of neighbours colors.

without meaning unkind, think have wrong idea how synchronization works in java.

there no way "lock object" in java. question isn't entirely clear there's odd infinite nesting of xs going on, x[1] ambiguous. essence if 1 object tries assign or read fields of another, succeed in reading/writing. way wait if chose synchronize on monitor beforehand. (this wait encapsulated of course, e.g. if c atomicreference<color> or similar - doesn't have done explicitly.)

also, if there multiple threads involved, visibility becomes issue well. have declare array of x, color variable, volatile else there's no guarantee updates seen other threads. alternatively, if choose go down synchronization route, , consistently, visibility guaranteed (though doesn't sound fits in ideas).

i not entirely sure you're trying achieve here can't give design advice. recommend book java concurrency in practice. concurrency hard, , it's essential head around if want avoid subtle bugs. moreso other area of development, requires understanding of principles upfront, rather muddling through issues come up.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -