Can a lock be acquired on a class?

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Does sprintf allocate memory?

582


What is composition in java?

715


Explain restrictions on using enum?

619


Is null or empty java?

522


How do you compare objects in java?

491






How do you check whether the list is empty or not in java?

505


How many types of equations are there?

533


How many digits can a float hold?

525


What does java se mean?

584


What about member inner classes?

617


What are design patterns and please explain?

563


What is the destroy method?

580


What is the difference between static class and normal class?

478


What is a method in java?

548


What is type conversion in java?

567