Can a lock be acquired on a class?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
Why hashcode is used in java?
why we r not use pointers in corejava?
Scenario: There are 1 to 100 numbers. Each number should be keep in the each column like from A column to Z column ie 1 to 26. From 27 to 52 should be in 2nd row in the excel sheet. This has to be continue till 100. How do you write Java program and what are various methods.
How do you use spaces in java?
How can you eliminate duplicates?
What is unmodifiable collection in java?
What is the r character?
What is structure of java heap? What is perm gen space in heap?
Why convert an applet to an application?
What is the integer of 16?
Java support what type of parameter passing ?
What is the difference between equals() and == in java?