Java 中允许方法锁定其父对象

发布于 2024-07-11 21:45:11 字数 333 浏览 7 评论 0原文

Java中有没有办法获得一个方法来锁定(互斥)它所在的对象?

我知道这听起来很令人困惑,但基本上我不需要 C# 的这段代码片段,而是 Java 中的代码片段。

lock(this)
{
    // Some code here...
}

我的任务是将 .Net 编写的 API 重新实现为 Java,并且要求我尽可能保持 Java 版本与 .Net 版本相似。 .Net 版本看起来像是从我无权访问的 C++ 版本转录而来,这一事实并没有帮助这一点。

无论如何,上面的行出现在 C# 版本中,我需要在 Java 中执行相同操作的东西。

Is there a way in Java to get a method to lock (mutex) the object which it is in?

I know this sounds confusing but basically I wan't an equivelent to this snippet of C# but in Java.

lock(this)
{
    // Some code here...
}

I've been tasked with reimplementing an API written in .Net into Java, and I've been asked to keep the Java version as similar to the .Net version as humanly possible. This isn't helped by the fact that the .Net version looked like it was transcribed from a C++ version which I don't have access to.

Anyway the above line appears in the C# version and I need something that does the same in Java.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

玩套路吗 2024-07-18 21:45:11

相当于:(

synchronized (this)
{
}

不,您通常不应该在 C# 或 Java 中执行此操作。更喜欢锁定其他任何人都无法访问的私有引用。当然,您可能已经意识到这一点 - 但我没有'不想在没有警告的情况下留下答案:)

The equivalent of that is:

synchronized (this)
{
}

(And no, you shouldn't generally do it in either C# or Java. Prefer locking on private references which nothing else has access to. You may be aware of that already, of course - but I didn't want to leave an answer without the warning :)

烟柳画桥 2024-07-18 21:45:11

假设 C++ 代码是一个简单的互斥锁,请将“lock”替换为“synchronized”,

synchronized (this)
{
// ...
}

这是 Java 并发教程了解更多信息

Assuming that the C++ code is a simple mutex, replace "lock" with "synchronized"

synchronized (this)
{
// ...
}

Here's the Java Concurrency tutorial for more info

百善笑为先 2024-07-18 21:45:11

我推荐 Brian Goetz 的“Java 并发实践”。 这是一本很棒的书。

保持同步块尽可能小可能是一件好事。 在方法上使用同步修饰符是粗粒度的,有时是必要的,但否则您可以使用另一个对象来执行此操作,以保持块更小。

像这样:

public class PrivateLock {
    private final Object myLock = new Object();
    @GuardedBy("myLock") Widget widget;

    void someMethod() {
        synchronized (myLock) {
            // Access or modify the state of widget
        }
    }
}

I'd recommend Brian Goetz's "Java Concurrency In Practice." It's an excellent book.

It can be a good thing to keep the synchronized block as small as possible. Using the synchronized modifier on the method is coarse-grained and sometimes necessary, but otherwise you can use another object to do it that keeps the block smaller.

Like this:

public class PrivateLock {
    private final Object myLock = new Object();
    @GuardedBy("myLock") Widget widget;

    void someMethod() {
        synchronized (myLock) {
            // Access or modify the state of widget
        }
    }
}
花开浅夏 2024-07-18 21:45:11

您还应该查看 API (JDK 5.0+) 的 java.util.concurrent 包以获取其他并发管理对象,例如信号量、交换器等

http://java.sun.com/j2se/1.5.0/docs/api/java/util/并发/package-summary.html

You should also look into the java.util.concurrent package of the API (JDK 5.0+) for additional concurrency management objects such as semaphore, exchanger, etc

http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/package-summary.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文