Can a lock be acquired on a class?

Answers were Sorted based on User's Feedback



Can a lock be acquired on a class?..

Answer / ravikiran(aptech mumbai)

yes indirectly because we can aquire a lock on the object of
the class.And for the class level lock we make use of static
synchromnized block.

Is This Answer Correct ?    8 Yes 2 No

Can a lock be acquired on a class?..

Answer / jamshed katta

A lock can be acquired on a class, you would want to do that if you are using static methods in your class, in which case there would be only one copy, so you would need a lock on the class rather than the instance.
Eg:

public static int getPageHits{ // Not synchronized
return hits;
}


so, now instead of using

public static int getPageHits{
synchronized(this){
return hits;
}
}


you would rather do


public static int getPageHits{

synchronized(ClassName.class){
return hits;
}

}

where ClassName is the name of the class whose lock u are trying to acquire.


you could also use Class.forName("ClassName")

Is This Answer Correct ?    5 Yes 0 No

Can a lock be acquired on a class?..

Answer / janet

Yes, a lock can be acquired on a class. This lock is
acquired on the class?s Class object.

Is This Answer Correct ?    4 Yes 0 No

Post New Answer

More Core Java Interview Questions

Convert Binary tree to linked list.

0 Answers   Amazon,


Implement a stack with push (), pop() and min() in O(1) time.

0 Answers   Amazon,


worst case complexities of Quick sort and Merge sort.

0 Answers   Amazon,


Why operator overloading is not in Java?

0 Answers  


What is difference between public static and void?

0 Answers  






What are the OOPS concepts in Java ?

4 Answers   Satyam,


What is a short in java?

0 Answers  


Does collectionutils isempty check for null?

0 Answers  


Which class is the superclass of all classes?

0 Answers  


Who is the owner of java?

0 Answers  


What is the access scope of a protected method?

0 Answers  


What are variable arguments or varargs?

0 Answers  


Categories