简体   繁体   中英

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() {
    synchronized(monitor) {
        do_something();
    }
}

In function_b , our thread "reenters" the monitor by declaring a second synchronization on it. Since the thread already owns the monitor, the thread can continue into the second synchronized block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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