我应该使用什么作为 Java 中同步语句的锁定对象 [英] What should I use as a lock object of a synchronized statement in Java

查看:42
本文介绍了我应该使用什么作为 Java 中同步语句的锁定对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下这些例子有什么区别?

Could anyone explain what is the difference between these examples?

示例 1.

public class Main {

    private Object lock = new Object();
    private MyClass myClass = new MyClass();

    public void testMethod() {
        // TODO Auto-generated method stub
        synchronized (myClass) {
            // TODO: modify myClass variable
        }
    }

}

示例 2.

package com.test;

public class Main {

    private MyClass myClass = new MyClass();
    private Object lock = new Object();

    public void testMethod() {
        // TODO Auto-generated method stub

        synchronized (lock) {
            // TODO: modify myClass variable
        }
    }

}

如果在修改变量时需要注意同步,我应该使用什么作为监视器锁?

What should I use as a monitor lock if I need to take care about synchronization when modifying the variable?

推荐答案

假设 Main 不是泄漏抽象",这里是第一个和第二个示例之间的最小差异.

Assuming that Main is not intended to be a "leaky abstraction", here is minimal difference between the first and second examples.

使用 Object 而不是其他类可能更好,因为 Object 实例没有字段,因此更小.Object-as-lock 习惯用法清楚地表明 lock 变量仅用作锁.

It may be better to use an Object rather than some other class because an Object instance has no fields and is therefore smaller. And the Object-as-lock idiom makes it clear that the lock variable is intended to only ever used as a lock.

话虽如此,锁定一个其他人永远看不到的对象有一定的优势.Main 方法在 Main(例如 this)上同步的问题是其他不相关的代码也可能出于不相关的目的同步它.通过同步专用(私有)锁定对象,您可以避免这种可能性.

Having said that, there is a definite advantage in locking on an object that nothing else will ever see. The problem with a Main method synchronizing on a Main (e.g. this) is that other unrelated code could also be synchronizing on it for an unrelated purpose. By synchronizing on dedicated (private) lock object you avoid that possibility.

回应评论:

这两种情况存在重大差异.首先,您要锁定要操作的对象.在第二个过程中,您锁定了与被操纵的对象没有明显关系的其他对象.第二种情况需要更多空间,因为您必须分配(否则未使用的)对象,而不是使用您正在保护的现有实例.

There is a MAJOR difference in the two cases. In the first you're locking the object that you want to manipulate. In the second you're locking some other object that has no obvious relationship to the object being manipulated. And the second case takes more space, since you must allocate the (otherwise unused) Object, rather than using the already-existing instance you're protecting.

我认为您做出了一个不正确的假设 - MyClass 是需要保护的数据结构.事实上,问题并没有这么说.实际上,示例的编写方式意味着锁旨在保护整个 Main 类……而不仅仅是其状态的一部分.在这种情况下,有一个明显的联系......

I think you are making an INCORRECT assumption - that MyClass is the data structure that needs protecting. In fact, the Question doesn't say that. Indeed the way that the example is written implies that the lock is intended to protect the entire Main class ... not just a part of its state. And in that context, there IS an obvious connection ...

最好锁定 MyClass 的唯一情况是 Main 是一个泄漏抽象,允许其他代码获取其 myClass 参考.那将是糟糕的设计,尤其是在多线程应用程序中.

The only case where it would be better to lock the MyClass would be if the Main was a leaky abstraction that allowed other code to get hold of its myClass reference. That would be bad design, especially in a multi-threaded app.

根据修订历史,我很确定这不是 OP 的意图.

Based on the revision history, I'm pretty sure that is not the OP's intention.

这篇关于我应该使用什么作为 Java 中同步语句的锁定对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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