Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

Why unicode is important?

978


What is style and indentation?

1057


What is use of a abstract variable?

1052


What are the advantages of java inner classes?

1115


Explain the reason behind ending a program with a system.exit(0)?

1132


Explain when noclassdeffounderror will be raised ?

1096


What is the maximum size of array in java?

1026


What does escaping a character mean?

1092


What is your platform?s default character encoding and how to know this?

2416


I have multiple constructors defined in a class. Is it possible to call a constructor from another constructor’s body?

1067


What is initial size of arraylist in java?

1100


Why we go for collections in java?

1027


What is a void method?

969


Does isempty check for null?

1073


What is continuity of a function?

1136