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

What is the difference between stringbuffer and stringbuilder?

1034


What is java and why do we need it? Explain

1113


Explain the concept of hashtables?

1112


What is meant by call by reference?

999


How to sort an unsorted array in java?

1114


What is the full meaning of java?

1033


Is char a data type in java?

957


How many bits are in a sentence?

1155


How do you define a parameter?

1024


When is an object subject to garbage collection?

1042


What is the set interface in java programming?

1191


Which collection is best for sorting in java?

1112


How to store image in arraylist in java?

962


What are the two parts of a conditional statement?

956


Is arraylist a class in java?

1048