Question:
Can a lock be acquired on a class?
Answer:
Yes, a lock can be acquired on a class. This lock is acquired on the class's Class object. Source: CoolInterview.com
The synchronized keyword tells the JVM that the method requires a lock in order to run. The JVM then creates the lock and manages the allocation of the lock to threads during execution. When a thread has the lock for a class, it is the only thread that can execute any of the class methods that require a lock. Source: CoolInterview.com
Answered by: sega | Date:
| Contact sega
You get a lock for the class by declaring the method to be synchronized, but the method also has to be a static method. a static method that is synchronized ensures a class level lock. whereas synchronizing a non-static method gives a lock on the object of that class Source: CoolInterview.com
Answered by: Vinaykumar | Date: 2/14/2009
| Contact Vinaykumar
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. Source: CoolInterview.com
Answered by: Rajani Nagandla | Date: 7/23/2009
| Contact Rajani Nagandla
we can acquire a lock on the class by using synchronization statement Source: CoolInterview.com
Answered by: vibha | Date: 6/6/2010
| Contact vibha
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|