当String参数用于锁定时,锁定在哪个对象上?

我在其中一个 repos 中遇到了这样的代码。我检查了它并且它有效。(只有一个线程进入同步块。)

public Void hello(String s) {

    synchronized (s) {

        i++;
        System.out.println(i);
    }

    return null;
}

我的问题是在 String 类本身上获得了锁吗?如果是,是否意味着如果这样的代码存在于代码库中的其他位置,它们都将等待获得对同一对象的锁定?从而增加不必要的延迟?

回答

String 对象的内在锁是获取的锁。但是锁定是否有效取决于字符串是否始终是同一个实例。字符串池和实习将影响这一点。

很难确定是否会使用相同的实例只是不这样做的一个原因。

如果应用程序中的两个类使用相同的字符串实例,则其中一个可以获取锁并将另一个关闭。所以你可以让概念上不相关的对象相互影响,争夺同一个锁。

人们也会困惑地认为他们可以使用字符串值来表示某些东西,并让代码更改同步块或方法中 s 的值。这将打破所有锁定。锁是在对象上,而不是在变量上。更改值意味着当前持有锁的线程现在拥有旧对象,但尝试进入的线程正在尝试获取新对象的锁,线程可以获取新对象并在前一个线程完成之前开始执行同步代码.

有时它可能会偶然起作用,但这是一个糟糕的主意。使用专用对象作为锁:

private final Object lock = new Object(); 

  • @akuzminykh when you have a construct like `synchronized(variable) { … }`, you are reading `variable` *before* entering the synchronized block (there’s no way around that). Hence, it doesn’t matter whether `variable` is changed inside or outside the synchronized block, as soon as there are concurrent modifications to `variable`, the read is racy and all bets are off. It doesn’t apply to the question’s code, as `s` is a local variable, but that’s actually worse. The caller could pass *anything* to the method and there’s no relationship between `s` and the variable `i` anyway. That’s just broken

以上是当String参数用于锁定时,锁定在哪个对象上?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>