What is a class level lock. Ca开发者_开发技巧n you please explain with an example.
I am assuming you are referring to a synchronization lock. If you are unfamiliar with synchronization it is a means to prevent the same code being run by two different threads at the same time. In java synchronization is done with Object locks:
Object lock = new Object();
public void doSomething(){
...
synchronized(lock){
//something dangerous
}
...
}
In this code it is guaranteed only one thread can do something dangerous at a given time, and it will complete everything in the synchronized block before another thread may start running the same code. I am guessing what you are referring to as a "class level lock" is the implicit lock on synchronized method:
public synchronized void somethingDangerous(){...}
Here again only one thread can execute the method at any given time and will always finish before another thread may begin executing the code. This is equivalent to a synchronized block on "this":
public void somethingDangerous(){
synchronized(this){
//something dangerous
}
}
Now this lock isn't actually on the class, but rather on a single instance (i.e. not all clocks, but only your clock). If you want a true "class level lock" (which would typically be done in a static method), then you need to synchronize on something independent of any instances. For example:
public class Clock{
private static Object CLASS_LOCK = new Object();
public static void doSomething(){
...
synchronized(CLASS_LOCK){
//something dangerous to all clocks, not just yours
}
...
}
}
again there is an implicit lock for static methods:
public class Clock{
public static synchronized void somethingDangerous(){}
}
which is the equivalent of locking on the class object:
public class Clock{
public static void somethingDangerous(){
synchronized(Clock.class){
//do something dangerous
}
}
}
If you use the synchronized
keyword on a static
method, the object whose monitor is used for the lock is the class object, i.e. the one represented by the literal MyClass.class
. It is also used implicitly to syncronize during the initialization of the class itself.
It's called an intrinsic lock, and every object has one. We need to separate two things, and it's not entirely clear what you mean by "class level lock".
Here's an example:
class Locker {
public void x() {
lock(this) { ... }
}
public void y() {
lock(this) { ... }
}
}
As you can see, both method uses "this" as a lock target - this way you can guarantee that they won't be ever interleaved. You can achieve this implicitly with the synchronization keyword:
class Locker {
public synchronized void x() {
...
}
public synchronized void y() {
...
}
}
The two examples are the same on the bytecode level.
However, by class lock, you could mean a lock on the actual class object - not an instance of it. That's how synchronized static methods work.
http://www.coderanch.com/t/232571/Threads-Synchronization/java/class-level-lock-object-level#1082419
synchronised on a static method :)
Class level lock and instance level lock both are different, mutually exclusive. Both don’t interfere each other lock status. If one instance of a class has already got locked by a thread then another thread can’t get lock for that instance until unless lock is freed by first thread.
Same behaviour is there for class level lock.
But if a thread acquires Class level lock then another thread can acquire lock on one of its instance. Both can work parallel.
精彩评论