• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Class Wide Lock..??

 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt regd Class wide locks..


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
 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 (/^_^\)
 
swati bannore
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 (/^_^\)
 
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<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
 
Geoffrey Falk
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic