Java threads calling parent functions -


im making software written in java, involves single 'parent' class invokes dozens of classes(nodes) run in own thread (extends thread).

the classes store parent class variable. if node class invokes method parent class, each node store copy of parent object or nodes act upon same object? if nodes act on same object, statements in called function run in thread of node or in thread of parent class?

ex code:

    class parent     {         arraylist<node> nodes = new  arraylist<node>();        void createstartnodes()        {           for(int =0; < 36;++i)           {              nodes.put(i,new node(this));              nodes.get(i).start();           }        }        void callsomefunc()        {            /* things */        }     } class node extends thread {    parent par;    public node(parent p)    {       par=p;    }   @override   public void run()   {      par.callsomefunc();   }  } 

does each node store copy of parent object or nodes act upon same object?

they each store pointer memory location of parent object. indeed act upon same object.

if nodes act on same object, statements in called function run in thread of node or in thread of parent class?

they run in thread of node tries modify parent object. may want make sure use synchronization when doing that, example using synchronized block. otherwise, when there concurrent access parent object, may incorrect / inconsistent behaviour. example:

class node extends thread {     parent par;      public node(parent p)     {        par=p;     }      @override     public void run()         {        synchronized(par) {            par.callsomefunc();        }     }  } 

the synchronized block give thread exclusive access parent object.

also, thread specific logic should in run method, not in start method.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -