可以使用同步方法在Java中实现可重入锁定机制[重复]

时间:2022-11-06 06:52:10

This question already has an answer here:

这个问题在这里已有答案:

Is there a way of using synchronized methods to create a mechanism for re-entrant locks?

有没有办法使用同步方法为可重入锁创建机制?

Since a synchronized method wont let 2 Threads to enter the critical section together i think it is not possible. Could anyone provide me some insight ?

由于同步方法不会让2个线程一起进入临界区,我认为这是不可能的。谁能为我提供一些见解?

Suppose i extended re-entrant property to Reader-Writer problem where multiple readers can have lock on same Object but Writer locks are exclusive.

假设我将重入属性扩展到Reader-Writer问题,其中多个读者可以锁定相同的Object但Writer锁是独占的。

Can we achieve this functionality using synchronized methods?

我们可以使用同步方法实现此功能吗?

3 个解决方案

#1


2  

Not sure if I understand the question, but a re-entrant lock is a lock that you can acquire a second time when you already hold it (as opposed to a non-re-entrant lock, that would just block at that point).

我不确定我是否理解了这个问题,但是一个可重入的锁是一个锁,当你已经拥有它时你可以第二次获得它(而不是一个非重入锁,那会阻止那个点)。

Synchronized blocks in Java have this property: A thread that already holds a lock can enter the block.

Java中的同步块具有以下属性:已经拥有锁的线程可以进入块。

Without this, it would be very hard to code proper execution paths, as you could not have one synchronized method of an object call another.

如果没有这个,那么编写正确的执行路径将非常困难,因为你不可能有一个对象的同步方法调用另一个。

#2


2  

In the following code

在以下代码中

public class Foo {
    public synchronized void bar() { 
        zoop();
    }

    public synchronized void zoop() {}
}

If a Thread calls

如果线程调用

Foo foo = new Foo();
foo.bar();

You have a reentrant lock, because the Thread owns the Foo object monitor and then reacquires it when it calls zoop() inside bar().

你有一个可重入的锁,因为Thread拥有Foo对象监视器,然后在它调用bar()里面的zoop()时重新获取它。

#3


1  

It isn't clear that you understand what 're-entrant' actually means. It means you can re-acquire a lock you already hold without being blocked on it. synchronized already has the re-entrancy property.

目前尚不清楚你了解“重入”实际意味着什么。这意味着您可以重新获得已经拥有的锁,而不会被锁定。 synchronized已经具有re-entrancy属性。

#1


2  

Not sure if I understand the question, but a re-entrant lock is a lock that you can acquire a second time when you already hold it (as opposed to a non-re-entrant lock, that would just block at that point).

我不确定我是否理解了这个问题,但是一个可重入的锁是一个锁,当你已经拥有它时你可以第二次获得它(而不是一个非重入锁,那会阻止那个点)。

Synchronized blocks in Java have this property: A thread that already holds a lock can enter the block.

Java中的同步块具有以下属性:已经拥有锁的线程可以进入块。

Without this, it would be very hard to code proper execution paths, as you could not have one synchronized method of an object call another.

如果没有这个,那么编写正确的执行路径将非常困难,因为你不可能有一个对象的同步方法调用另一个。

#2


2  

In the following code

在以下代码中

public class Foo {
    public synchronized void bar() { 
        zoop();
    }

    public synchronized void zoop() {}
}

If a Thread calls

如果线程调用

Foo foo = new Foo();
foo.bar();

You have a reentrant lock, because the Thread owns the Foo object monitor and then reacquires it when it calls zoop() inside bar().

你有一个可重入的锁,因为Thread拥有Foo对象监视器,然后在它调用bar()里面的zoop()时重新获取它。

#3


1  

It isn't clear that you understand what 're-entrant' actually means. It means you can re-acquire a lock you already hold without being blocked on it. synchronized already has the re-entrancy property.

目前尚不清楚你了解“重入”实际意味着什么。这意味着您可以重新获得已经拥有的锁,而不会被锁定。 synchronized已经具有re-entrancy属性。