Professional Documents
Culture Documents
Java FAQ
Recent Topics
Register / Login
Author
hi all, I am not sure I am getting the idea of how synchronized static methods. synchronized blocks and synchronized static blocks works correctly For synchronized static methods, as far as I understand every class has its own lock through its corresponding instance of java.lang.Class...which means that no matter how many objects do exist for the class that do have the synchhronized static methods, only ONE lock can be done at a time on the synchronized static methods of the class through its corresponding instance of java.lang.Class Now what I am confused about in the classes which contains synchronized static methods or to be more specific in classes that contain synchronized static methods, synchronized methods, synchronized static blocks AND synchronized blocks in ONE class...what will happen for example if a thread gained lock on the synchronized static method of this class...can other threads access the synchronized methods, synchronized static blocks, synchronized blocks of this same class?
https://www.coderanch.com/t/269591/java-programmer-SCJP/certification/synchronized-methods-blocks-worries
1/7
7/14/13
For instance and static blocks, I really don't understand how their blocking mechanism work, also the object on which the lock will happen so I will appreciate it so much if someone can please help me by explaining those two parts as well Thanks in advance for your time and efforts
Mahmoud Metwally<br />Preparing for SC JP...<br /> <br />"Try not to become a man of success but a man of value" - Albert Einstein
kaushik vira Ranch Hand Joined: Feb 01, 2007 Posts: 102
It`s not necessary that lock could be on own lock through its corresponding instance of java.lang.Class... refer flowing example. public class Sync {
public static void main(String[] args) { } //case 1 public void syncBolckWithClassLock(){ synchronized (Sync.class) { } } //case 2 public void syncBolckWithObjectLock(){ synchronized (this) { } } //case 3 - same behaviour like case 2. public synchronized void syncMethodWithObjectLock(){} }
each synchronization having one monitor object, in synchronize block you will explicitly define that monitor object. But in synchronize method it`s default 'this'. now other object can get lock on synchronize part or not it`s depend on you monitor object. case 1:- monitor object is Sync.class so onces any Thread enter in this part
https://www.coderanch.com/t/269591/java-programmer-SCJP/certification/synchronized-methods-blocks-worries 2/7
7/14/13
other one can`t acquire lock. case 2 and 3 :- but in if you call method form different object this is not locked you will acquire the lock. try to Run following 3 example one by one.. and observer output
kaushik vira Ranch Hand Joined: Feb 01, 2007 Posts: 102
static Sync s1 = new Sync(); static Sync s2 = new Sync(); //case 1 public void syncBolckWithClassLock() throws Exception{ synchronized (Sync.class) { System.out.println("syncBolckWithObjectLock locked by" + Thread.currentThread().getName()); Thread.currentThread().sleep(Long.MAX_VALUE); } } //case 2 public void syncBolckWithObjectLock() throws Exception{ synchronized (this) { System.out.println("syncBolckWithObjectLock locked by" + Thread.currentThread().getName()); Thread.currentThread().sleep(Long.MAX_VALUE); } } //case 3 - same behaviour like case 2. public synchronized void syncMethodWithObjectLock() throws Exception{ System.out.println("syncBolckWithObjectLock locked by" + Thread.currentThread().getName()); Thread.currentThread().sleep(Long.MAX_VALUE); }
https://www.coderanch.com/t/269591/java-programmer-SCJP/certification/synchronized-methods-blocks-worries
3/7
7/14/13
Thread t1 = new Thread(){ @Override public void run(){ try{ s1.syncBolckWithClassLock(); }catch(Exception e){ e.printStackTrace(); } } }; t1.setName("t1"); t1.start(); Thread t2 = new Thread(){ @Override public void run(){ try{ s2.syncBolckWithClassLock(); }catch(Exception e){ e.printStackTrace(); } } }; t2.setName("t2"); t2.start(); Thread.currentThread().sleep(Long.MAX_VALUE); } }
kaushik vira Ranch Hand Joined: Feb 01, 2007 Posts: 102
static Sync s1 = new Sync(); static Sync s2 = new Sync(); //case 1 public void syncBolckWithClassLock() throws Exception{ synchronized (Sync.class) { System.out.println("syncBolckWithObjectLock locked by" + Thread.currentThread().getName());
https://www.coderanch.com/t/269591/java-programmer-SCJP/certification/synchronized-methods-blocks-worries 4/7
7/14/13
Thread.currentThread().sleep(Long.MAX_VALUE); } } //case 2 public void syncBolckWithObjectLock() throws Exception{ synchronized (this) { System.out.println("syncBolckWithObjectLock locked by" + Thread.currentThread().getName()); Thread.currentThread().sleep(Long.MAX_VALUE); } } //case 3 - same behaviour like case 2. public synchronized void syncMethodWithObjectLock() throws Exception{ System.out.println("syncBolckWithObjectLock locked by" + Thread.currentThread().getName()); Thread.currentThread().sleep(Long.MAX_VALUE); }
public static void main(String [] args) throws Exception{ Thread.currentThread().setName("Main"); Thread t1 = new Thread(){ @Override public void run(){ try{ s1.syncBolckWithObjectLock(); }catch(Exception e){ e.printStackTrace(); } } }; t1.setName("t1"); t1.start(); Thread t2 = new Thread(){ @Override public void run(){ try{ s2.syncBolckWithObjectLock(); }catch(Exception e){ e.printStackTrace(); } } }; t2.setName("t2"); t2.start(); Thread.currentThread().sleep(Long.MAX_VALUE);
https://www.coderanch.com/t/269591/java-programmer-SCJP/certification/synchronized-methods-blocks-worries
5/7
7/14/13
} }
kaushik vira Ranch Hand Joined: Feb 01, 2007 Posts: 102
static Sync s1 = new Sync(); static Sync s2 = new Sync(); //case 1 public void syncBolckWithClassLock() throws Exception{ synchronized (Sync.class) { System.out.println("syncBolckWithObjectLock locked by" + Thread.currentThread().getName()); Thread.currentThread().sleep(Long.MAX_VALUE); } } //case 2 public void syncBolckWithObjectLock() throws Exception{ synchronized (this) { System.out.println("syncBolckWithObjectLock locked by" + Thread.currentThread().getName()); Thread.currentThread().sleep(Long.MAX_VALUE); } } //case 3 - same behaviour like case 2. public synchronized void syncMethodWithObjectLock() throws Exception{ System.out.println("syncBolckWithObjectLock locked by" + Thread.currentThread().getName()); Thread.currentThread().sleep(Long.MAX_VALUE); }
public static void main(String [] args) throws Exception{ Thread.currentThread().setName("Main"); Thread t1 = new Thread(){ @Override public void run(){ try{ s1.syncMethodWithObjectLock(); }catch(Exception e){ e.printStackTrace(); }
https://www.coderanch.com/t/269591/java-programmer-SCJP/certification/synchronized-methods-blocks-worries 6/7
7/14/13
} }; t1.setName("t1"); t1.start(); Thread t2 = new Thread(){ @Override public void run(){ try{ s2.syncMethodWithObjectLock(); }catch(Exception e){ e.printStackTrace(); } } }; t2.setName("t2"); t2.start();
Thread.currentThread().sleep(Long.MAX_VALUE); } }
Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
Similar Threads regarding static method Thread Locking a Object - Doubt chapter 9 self test Q.15 What is meaning of a static method to synchronized??? questions
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 14, 2013 09:08:19 .
https://www.coderanch.com/t/269591/java-programmer-SCJP/certification/synchronized-methods-blocks-worries
7/7