java - Where do i put the synchronization -


lets have method deals shared variable. due complexity of method part of needs encapsulated method.

so have this:

private void methoda() {     //do something...     //do else...      synchronized(shared_variable)     {         //do something...         methodb(shared_variable);         //.....         //.....     }  } 

now i'd know if should synchronize shared_variable in methodb or not. know not necessary code somehow smells badly.

any solution how things in such kind of situations?

p.s.

the reason asking because later in developement process methodb may called outside of methoda. if not put symchronization on shared_variable in methodb wont thread safe. when call methodb methoda have locked shared_variable twice looks litte strange me.

by declaring synchronized(shared_variable), declare monitor on shared_variable, means whenever thread enters synchronized block, acquires lock on given object. theoretically, no other thread able achieve monitor, until released initial thread, happens when completes execution of synchronized block (which includes execution of methodb).


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -