java - What is the purpose of passing parameter to synchronized block? -


i know that

when synchronize block of code, specify object's lock want use lock, could, example, use third-party object lock piece of code. gives ability have more 1 lock code synchronization within single object.

however, don't understand need of passing argument block. because doesn't matter whether pass string's instance, random class's instance synchronized block synchronized block works irrespective of parameter being passed block.

so question if anyways synchronized block stops 2 threads entering critical section simultaneously. why there need of passing argument. (i mean acquire lock on random object default).

i hope framed question correctly.

i have tried following example random parameters being synchronized block.

public class launcher {      public static void main(string[] args) {         accountoperations accops=new accountoperations();          thread lucy=new thread(accops,"lucy");         thread sam=new thread(accops,"sam");          lucy.start();         sam.start();      }  } 

using non-static synchronized block:

public class accountoperations implements runnable{     private  account account = new account();       public void run(){          for(int i=0;i<5;i++){              makewithdrawal(10);                          }     }      public  void makewithdrawal(int amount){         string str="asd"         synchronized (str /* pass non-null object synchronized block works*/) {             if(account.getamount()>10){                  try{                     thread.sleep(5000);                              }catch(interruptedexception e){                     e.printstacktrace();                 }                 account.withdraw(amount);                 system.out.println(thread.currentthread().getname()+" has withdrawn 10, current balance "+ account.getamount());             }else{                 system.out.println("insufficient funds "+account.getamount());             }         }      }  } 

using static synchronized block:

public class accountoperations implements runnable{     private static account account = new account();       public void run(){          for(int i=0;i<5;i++){              makewithdrawal(10);                          }     }      public static void makewithdrawal(int amount){          synchronized (string.class /* pass class literal synchronized block works*/) {             if(account.getamount()>10){                  try{                     thread.sleep(5000);                              }catch(interruptedexception e){                     e.printstacktrace();                 }                 account.withdraw(amount);                 system.out.println(thread.currentthread().getname()+" has withdrawn 10, current balance "+ account.getamount());             }else{                 system.out.println("insufficient funds "+account.getamount());             }         }      }  } 

if anyways synchronized block stops 2 threads entering critical section simultaneously. why there need of passing argument?

synchronized block decides threads stop based on object pass it. object pass serves identifier of critical section guarded synchronized block.

you may have many critical sections in program, of executed concurrently each other. example, if have 2 unrelated collections must accessed concurrently, can set separate critical sections each collection. way threads stopped when other threads accessing same collection; 2 different threads accessing 2 different collections allowed proceed concurrently.

your first example non-trivial. reason works string object initialized string literal. due literal's interning, threads entering function obtain same string object, synchronized block guard critical section.


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 -