java - Synchonization with static block -


i have question: can use static keyword synchronized method? know static related class , synchronization used block object, using synchronized static doesn't make sense me. why , in situation use synchronization static keyword?

i think you:

for not familiar static synchronized methods locked on class object e.g. string class string.class while instance synchronized method locks on current instance of object denoted this.

since both of these object different have different locks, while 1 thread executing static synchronized method, other thread doesn’t need wait thread return. instead acquire separate lock denoted .class literal , enter static synchronized method.

this popular multi-threading interview questions interviewer asked on lock particular method gets locked, time appear in java test papers.

an example:

public class synchornizationmistakes {     private static int count = 0;      //locking on object lock     public synchronized int getcount(){         return count;     }      //locking on .class object lock     public static synchronized void increment(){         count++;     }  } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -