Issue
if I have two methods:
public class A {
public void doSomething() {
synchronized(A.class) {
//synchronized block here
}
}
public void doSomethingElse() {
synchronized(A.class) {
//synchronized block here
}
}
}
So my question is, does the A.class act like a global lock? Meaning will one thread executing method doSomething() be blocked while another thread is executing doSomethingElse()?
Thank you.
Solution
Your example will lock threads out of all methods that acquire the lock in all instances of the A class that are loaded by the same classloader, so if any one thread acquires the lock that blocks all other threads from accessing any of those methods on that object or any other object of that class. Typically setting up something like this would likely be a bad idea. The lock will be unnecessarily coarse-grained (causing threads to wait even if they only want to access data belonging to different instances where there would be no sharing) and will restrict concurrency excessively. If you have a design that actually needs this you should question your design.
Answered By - Nathan Hughes
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.