简体   繁体   English

在Java中关于线程中的同步和监视

[英]In Java about Synchronization and monitor in Thread

I read that a thread that owns a monitor can reenter the same monitor if it need to do so, how that can be achieved. 我读到拥有一个监视器的线程可以在需要时重新输入同一监视器,这是如何实现的。 Any help will be greatly appreciated. 任何帮助将不胜感激。

The following function takes ownership of a monitor: 以下功能获得监视器的所有权:

function_a() {
    synchronized(monitor) {
        function_b();
    }
}

Now let's say function_b looks like this: 现在让我们说function_b看起来像这样:

function_b() {
    synchronized(monitor) {
        do_something();
    }
}

In function_b , our thread "reenters" the monitor by declaring a second synchronization on it. function_b ,我们的线程通过在监视器上声明第二次同步来“重新进入”该监视器。 Since the thread already owns the monitor, the thread can continue into the second synchronized block. 由于线程已经拥有监视器,因此线程可以继续进入第二个同步块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM