Java 线程和同步块

发布于 2024-10-21 19:03:37 字数 887 浏览 2 评论 0原文

假设我正在某个线程内执行一个同步代码块,并且在同步代码块内我调用一个方法,该方法生成另一个线程来处理需要同步代码块的方法。与第一种方法相同的锁。所以在伪 Java 代码中:

public void someMethod() {
  synchronized(lock_obj) {
    // a whole bunch of stuff...

    // this is the last statement in the block
    (new Thread(someOtherMethod())).start();
  }
    // some more code that doesn't require a lock
}

public void someOtherMethod() {
  // some setup code that doesn't require a lock

  // return the stuff we want to run in another thread
  // that does require a lock
  return new Runnable() {
    @Override
    public void run() {
      synchronized(lock_obj) {
        // some more code
      }
    }
  };
}

我不知道如何理解该代码。我写的东西合法吗?从语法上讲,我没有看到任何问题,但我不确定如何通过这样的代码进行推理。那么,当我执行 someOtherMethod() 以创建 Runnable 实例时,return 语句之前的代码会在什么样的范围内运行?它是否作为第一个同步块的一部分执行?假设还有一些其他线程也在工作,可能需要锁定 lock_obj

Suppose I'm executing a synchronized block of code inside some thread and within the synchronized block I call a method that spawns another thread to process a synchronized block of code that requires the same lock as the first method. So in pseudo Java code:

public void someMethod() {
  synchronized(lock_obj) {
    // a whole bunch of stuff...

    // this is the last statement in the block
    (new Thread(someOtherMethod())).start();
  }
    // some more code that doesn't require a lock
}

public void someOtherMethod() {
  // some setup code that doesn't require a lock

  // return the stuff we want to run in another thread
  // that does require a lock
  return new Runnable() {
    @Override
    public void run() {
      synchronized(lock_obj) {
        // some more code
      }
    }
  };
}

I have no idea how to make sense of that code. Is what I have written even legal? Syntactically I don't see any issues but I'm not sure how to reason through code like that. So when I execute someOtherMethod() in order to create an instance of Runnable in what kind of scope does the code before the return statement run? Does it execute as part of the first synchronized block? Assume there are some other threads working as well that might require the lock on lock_obj.

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

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

发布评论

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

评论(3

羁〃客ぐ 2024-10-28 19:03:37

在创建可运行对象和线程期间,您仍然持有锁,但是在调用 start 之后和线程实际获取之前,您将放弃锁。新线程将不得不与其他线程竞争锁。

You are still holding the lock during the creation of runnable and the thread, but after you call start and before the thread actually picks up you are relinquishing the lock. The new thread will have to compete for the lock with other threads.

白馒头 2024-10-28 19:03:37

这段代码没有任何问题。在someOtherMethod()中的return语句之前,代码运行在someMethod()的同步块中。新线程启动后,它将阻塞在 run() 方法内的 synchronized 语句上,直到获得 lock_obj 上的锁(在最早,每当 someMethod() 退出其同步块时)。

There's nothing wrong about this code. Before the return statement in someOtherMethod(), the code is running in the synchronized block of someMethod(). After the new thread starts, it will block on the synchronized statement inside the run() method until it obtains a lock on lock_obj (at the earliest, whenever someMethod() exits its synchronized block).

池木 2024-10-28 19:03:37

如果首先调用 someMethod(),则这是死锁的典型示例。

我写的内容合法吗?
---- 是的,它在语法上是完全合法的。

那么,当我执行 someOtherMethod() 以创建 Runnable 实例时,return 语句之前的代码会在什么样的范围内运行?
----如果 someOtherMethod() 是从 someMethod() 内部调用的,那么它就在 someMethod() 方法的同步块的范围内。

If someMethod() is invoked first, its the classical example of a deadlock.

Is what I have written even legal?
---- Yes it is perfectly legal syntactically.

So when I execute someOtherMethod() in order to create an instance of Runnable in what kind of scope does the code before the return statement run?
----If the someOtherMethod() is invoked from within someMethod() then its in scope of the synchronized block of the someMethod() method.

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