如何在java中实现类级别锁定? [英] How is class level locking achieved in java?

查看:140
本文介绍了如何在java中实现类级别锁定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道锁定概念与静态和非静态方法的同步分别锁定类和实例。我无法理解的是,如何实现班级锁定?我的意思是,课程只是一个模板,没有任何物理意义。那么,当我们说通过同步静态方法实现类级别锁定时会发生什么呢?该类的所有对象是否被锁定或其他进程?

I am aware of locking concepts with synchronization of static and non static methods to lock classes and instances respectively. What I am not able to understand is, how is class level lock achieved? I mean, class is a template only, with no physical significance. So, when we say that class level locking is achieved with synchronizing static methods what happens then? Do all the objects of that class get locked or some other process?

我可以通过搜索找到的是有类对象(Class.class)并且在此类对象上获取了锁。但那么该类的所有实例也是如何锁定的?

With what I could find out with my search is that there are class objects (Class.class) and lock is acquired on this class object. But then how are all instances of that class also locked?

推荐答案


执行该类的所有对象class被锁定还是其他一些过程?

Do all the objects of that class get locked or some other process?

首先,让我们谈谈锁定一个对象意味着什么。

First, let's talk about what what it means to "lock" an object.

Foobar foobar = new Foobar();

synchronized (foobar) {
    ...
}

当线程在 synchronized 块中时,您可能会说foobar对象被锁定。但这对该计划有什么作用?许多新手错误地认为它会阻止其他线程访问该对象。但是,那是是真的。什么 synchronized 确实 - 同步的唯一功能 - 是保证同一个对象上不能同时同步一个以上的线程。

You might say that the foobar object is "locked" when a thread is in the synchronized block. But what does that do for the program? A lot of newbies make the mistake of thinking that it will prevent other threads from accessing the object. But, that is not true. What synchronized does--the only thing synchronized does--is to guarantee that no more than one thread can be synchronized on the same object at the same time.

程序员在上面的示例中的意图可能是阻止其他线程在不一致的状态下看到foobar。在这种情况下,访问foobar的每个方法和每个代码片段都必须在foobar上同步。想象foobar是一个有很多门的大房间。使用foobar的每种方法都像一扇不同的门。如果你想让人们离开房间,只能锁上一扇门是没有用的。你必须锁定所有

The programmer's intent in the example above might be to prevent other threads from seeing foobar in an inconsistent state. In that case, every method and every fragment of code that accesses foobar must be synchronized on foobar. Imagine foobar as big room with many doors. Each method that uses foobar is like a different door. If you want to keep people out of the room, it doesn't help to lock just one door. You have to lock all of them.

现在,问题是:

当我们说同步静态方法实现类级别锁定时会发生什么?

when we say that class level locking is achieved with synchronizing static methods what happens then?

简单。这:

class Foobar {
    static synchonized void do_something() {
        ...
    }
}

与此完全相同:

class Foobar {
    static void do_something() {
        synchronized(Foobar.class) {
            ...
        }
    }
}

您始终在对象上进行同步。好吧,类一个对象。当 static 方法 synchronized 时,这意味着方法体在类对象上同步。

You always synchronize on an Object. Well, a class is an Object. When a static method is synchronized, that just means that the method body is synchronized on the class object.

由于类是 singleton 对象,这意味着没有两个线程可以同时进入同一个静态同步方法。在我之前的例子中,变量foobar可以在不同的时间引用不同的对象,但在静态示例中,保证Foobar.class始终引用相同的单例。

Since a class is a singleton object, that means that no two threads can get into the same static synchronized method at the same time. In my earlier example, the variable foobar could refer to different objects at different times, but in the static example, Foobar.class is guaranteed always to refer to the same singleton.

编辑:正如@Danny指出的那样,在Foobar类上同步的块(我的第二个例子)与在<上同步的块之间没有连接Foobar类的em> instance (我的第一个例子)。实例和类对象是两个不同的对象,因此没有什么能阻止一个线程在实例上进行同步,而另一个线程在类上同步。同样,没有什么可以防止两个不同的线程在两个不同的实例上同步。新手经常犯的另一个错误是认为一次只有一个线程可以进入 synchronized 块:

As, @Danny pointed out, there is no connection between a block that is synchronized on the Foobar class, (my second example) and a block that is synchronized on an instance of the Foobar class (my first example). The instance and the class object are two different objects, so nothing prevents one thread from synchronizing on the instance while another thread is synchronized on the class. Likewise, nothing prevents two different threads from synchronizing on two different instances. Another mistake that newbies often make is to think that only one thread at a time can enter this synchronized block:

Integer n = ...;

synchronized (n) {
    n = n+1;
    ...
}

但事实并非如此。它不是被锁定的变量n,它是Integer类的特定实例。进入块的每个线程都会创建一个新的Integer实例并将其分配给n。因此,当下一个线程出现时,n不再引用第一个线程已同步的相同实例。

But it's not true. It's not the variable, n, that is locked, it's a particular instance of the Integer class. Each thread that enters the block creates a new Integer instance and assigns it to n. So when the next thread comes along, n no longer refers to the same instance that the first thread has synchronized.

这篇关于如何在java中实现类级别锁定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆