public class AQueryClass { public static synchronized void method1() { //lots and lots of code here. } public sychronized void method2() { //lots of code here too. } }
Correctme if my assumtions are wrong.. 1) In the ablove code,there is a Class level Lock cause static method is synchronized. 2) If,Thread t1,acquires a lock on method m1(), it has a lock on m2 also.. 3) Are these two locks different or same ..m1 and m2?? 4) Is there a lock for instance / static variables.. Help Thanx
Swati Kale
SCJP
SCWCD
Gagan Indus
Ranch Hand
Joined: Feb 28, 2001
Posts: 346
posted
0
well , as per my understanding 1) Yes , Above class hav a class level lock , but a static method is synchronized , tht is not da reason. Even widout this sync method , class lock will be there . 2) NO , if Thread acquire lock on m1() , dat 'll be Object lock , which is diff from Class lock. 3) Yes , Class lock n Object Lock are diff. 4) There is no lock for varibles , these both locks( class or object lock ) are used for syncronization wid : - synchronized methods ( class or instance methods ) - synchronized code ( like synchronized(Object) )
------------------ Gagan (/^_^\)
Gagan (/^_^\) SCJP2 SCWCD IBM486 <br />Die-hard JavaMonk -- little Java a day, keeps you going.<br /><a href="http://www.objectfirst.com/blog" target="_blank" rel="nofollow">My Blog</a>
swati bannore
Ranch Hand
Joined: Oct 18, 2000
Posts: 201
posted
0
Thanx Gagan, I got confused about Class level lock.Class level lock is obtained when we say , synchronized(this); Then,how come the above code has a class level lock.. Thanx
Gagan Indus
Ranch Hand
Joined: Feb 28, 2001
Posts: 346
posted
0
sorry Swati , one correction in my earlier post i said " 2)NO , if Thread acquire lock on m1() , dat 'll be Object lock , which is diff from Class lock. " which shld be read as "2)NO , if Thread acquire lock on m1() , dat 'll be Class lock , which is diff from Object lock." now coming to synchronized(this) , in this case even the Object's Lock is being used ! so : public synchronized void method() { // do lotz of things here } and : public void method() { synchronized(this) { // do lotz of things here } } are equivalent , in da sense dat they both are being synchronized using da same Object-lock. ( if it would have been synchronized(object1) instead , then object1's lock will be used instead of Object-lock of da current object , this dat is ) well , class-lock is used for static-methods only ------------------ Gagan (/^_^\)
Geoffrey Falk
Ranch Hand
Joined: Aug 17, 2001
Posts: 171
posted
0
<PRE> public class AQueryClass public static synchronized void method1()</PRE> Static method synchronizes on the Class object representing AQueryClass. <PRE>public sychronized void method2()</PRE> Non-static method synchronizes on an object that is an instance of AQueryClass. So to answer your question, these locks are on different objects and therefore they are totally independent of each other. Geoffrey ------------------ Sun Certified Programmer for the Java 2 Platform
Sun Certified Programmer for the Java 2 Platform
Geoffrey Falk
Ranch Hand
Joined: Aug 17, 2001
Posts: 171
posted
0
Try this code.. You will see that the synchronized methods can be entered concurrently because they are using different locks. <PRE> class Funky implements Runnable { public static void main(String[] args) { Thread ravel = new Thread(new Funky()); ravel.start(); method1(); } public synchronized void run() { System.out.println("entered nonstatic synchronized method"); try { Thread.sleep(1000); } catch (InterruptedException e) {} System.out.println("exited nonstatic synchronized method"); } public static synchronized void method1() { System.out.println("entered static synchronized method"); try { Thread.sleep(1000); } catch (InterruptedException e) {} System.out.println("exited static synchronized method"); } } </PRE> Geoffrey ------------------ Sun Certified Programmer for the Java 2 Platform