java中的类级锁是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2462104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:03:00  来源:igfitidea点击:

What is a class level lock in java

javamultithreading

提问by sap

What is a class level lock. Can you please explain with an example.

什么是类级锁。能否请您举例说明。

采纳答案by M. Jessup

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:

我假设您指的是同步锁。如果您不熟悉同步,它是一种防止相同代码同时由两个不同线程运行的方法。在 Java 中,同步是通过对象锁完成的:

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":

同样,在任何给定时间只有一个线程可以执行该方法,并且总是在另一个线程开始执行代码之前完成。这相当于“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
    }
  }
}

回答by Michael Borgwardt

If you use the synchronizedkeyword on a staticmethod, 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.

如果synchronizedstatic方法上使用关键字,则其监视器用于锁定的对象是类对象,即由文字 表示的对象MyClass.class。它也隐式地用于在类本身的初始化期间进行同步。

回答by Zoltan

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:

如您所见,这两种方法都使用“this”作为锁定目标 - 这样您就可以保证它们不会被交错。您可以使用同步关键字隐式实现此目的:

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 couldmean a lock on the actual class object - not an instance of it. That's how synchronized static methods work.

但是,通过类锁,您可能意味着对实际类对象的锁 - 而不是它的实例。这就是同步静态方法的工作原理。

回答by rajvineet

Class level lockand instance level lockboth 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.

类级别 lock也有相同的行为。

But if a thread acquires Class level lockthen another thread can acquire lock on one of its instance. Both can work parallel.

但是如果一个线程获得了类级别的锁,那么另一个线程可以在它的一个实例上获得锁。两者可以并行工作。