Sunteți pe pagina 1din 7

7/14/13

synchronized methods and blocks worries (SCJP forum at JavaRanch)

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Certification Programmer Certification (SCJP/OCPJP)

Author

synchronized methods and blocks worries


posted 7/22/2008 3:51 AM

Mahmoud Metwaly Greenhorn Joined: Jul 16, 2008 Posts: 14

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

synchronized methods and blocks worries (SCJP forum at JavaRanch)

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

posted 7/22/2008 12:17 PM

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

synchronized methods and blocks worries (SCJP forum at JavaRanch)

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 ------------------------------------SC JP, Preparing SC WC D..

kaushik vira Ranch Hand Joined: Feb 01, 2007 Posts: 102

posted 7/22/2008 12:20 PM

/*case 1*/ public class Sync {

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

synchronized methods and blocks worries (SCJP forum at JavaRanch)

public static void main(String [] args) throws Exception{ Thread.currentThread().setName("Main");

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

posted 7/22/2008 12:21 PM

/*case 2*/ public class Sync {

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

synchronized methods and blocks worries (SCJP forum at JavaRanch)

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

synchronized methods and blocks worries (SCJP forum at JavaRanch)

} }

kaushik vira Ranch Hand Joined: Feb 01, 2007 Posts: 102

posted 7/22/2008 12:23 PM

/*case 3*/ public class Sync {

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

synchronized methods and blocks worries (SCJP forum at JavaRanch)

} }; 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

subject: synchronized methods and blocks worries

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 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

https://www.coderanch.com/t/269591/java-programmer-SCJP/certification/synchronized-methods-blocks-worries

7/7

S-ar putea să vă placă și