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

Give us the name of the list layoutmanagers in java?

927


Can java inner class be static?

1028


can java object be locked down for exclusive use by a given thread? : Java thread

1103


What is a double?

1100


1.IN CASE OF DYNAMIC METHOD DISPATCH WHY WE USE REFERENCE VARIABLE,WE CAN USE THE DIFFERENT DEFINED OBJECT DIRECTLY TO ACCESS THE DATA MEMBER AND MEMBER FUNCTION OF THAT RESPECTIVE CLASS?WHAT IS THE MAIN FUNCTION OF "REFERENCE VARIABLE" HERE?

4961


How can you add and remove nodes in jtree?

1044


Which container method is used to cause a container to be laid out and redisplayed in java programming?

1109


What is method overloading in JAVA? Why is it not present in C ?

1103


What is passing by reference in java?

1027


What is the disadvantage of synchronization?

1017


What is the difference between iterator and list iterator?

1038


What is the Difference between Final Class && Abstract Class?

1078


Explain about static imports in java?

999


What is use of static in java?

1176


What is collection sort in java?

952