java中的“重入” [英] 'Reentrancy' in java

查看:97
本文介绍了java中的“重入”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重入意味着在每个线程而不是每次调用的基础上获取锁。

Reentrancy means that locks are acquired on a per-thread rather than per-invocation basis.

由于线程持有内部锁,不是吗是否一个线程运行一次等于一个调用基础?

Since an intrinsic lock is held by a thread, doesn't it mean that a thread run once equals an invocation basis?

谢谢,这似乎意味着:在一个线程中,如果我得到一个锁 lockA 当处理函数 doA 调用函数 doB doB时还需要一个锁 lockA ,那么就会有一次重入。在Java中,每个线程都会获得这种现象,所以我不需要考虑死锁?

Thank you, it seems mean that: in a thread,if I get a lock lockA when process function doA which call function doB, and doB also need a lock lockA,then there wil be a reentrancy. In Java, this phenomenon is acquired per thread, so I needn't consider deadlocks?

推荐答案


重入意味着在每个线程而不是每次调用的基础上获取锁。

Reentrancy means that locks are acquired on a per-thread rather than per-invocation basis.

这是一个误导性的定义。这是真的(有点),但它错过了真正的观点。

That is a misleading definition. It is true (sort of), but it misses the real point.

重入意味着(通常是CS / IT术语)你做了什么,当你还在这样做,你再做一次。在锁定的情况下,它意味着您在单个线程上执行类似的操作

Reentrancy means (in general CS / IT terminology) that you do something, and while you are still doing it, you do it again. In the case of locks it means you do something like this on a single thread:


  1. 获取锁定foo。

  2. 做点什么

  3. 获取foo的锁定。请注意,我们尚未发布之前获得的锁定。

  4. ...

  5. 释放锁定foo

  6. ...

  7. 释放锁定foo

  1. Acquire a lock on "foo".
  2. Do something
  3. Acquire a lock on "foo". Note that we haven't released the lock that we previously acquired.
  4. ...
  5. Release lock on "foo"
  6. ...
  7. Release lock on "foo"

随着一个可重入的锁定/锁定机制,获取相同锁定的尝试将成功,并将增加属于该锁定的内部计数器。仅当锁的当前持有者已经释放它两次时才会释放锁。

With a reentrant lock / locking mechanism, the attempt to acquire the same lock will succeed, and will increment an internal counter belonging to the lock. The lock will only be released when the current holder of the lock has released it twice.

这是Java中使用原始对象锁/监视器的一个例子......它们是可重入的:

Here's a example in Java using primitive object locks / monitors ... which are reentrant:

Object lock = new Object();
...
synchronized (lock) {
    ...
    doSomething(lock, ...)
    ...
}

public void doSomething(Object lock, ...) {
    synchronized (lock) {
        ...
    }
}

可重入的替代方法是非重入锁定,其中线程尝试获取已经锁定的错误将是错误的保留。

The alternative to reentrant is non-reentrant locking, where it would be an error for a thread to attempt to acquire a lock that it already holds.

使用重入锁定的优势在于,您不必担心因意外获取已锁定的锁定而导致失败的可能性。缺点是您不能假设您调用的任何内容都不会更改锁定旨在保护的变量的状态。但是,这通常不是问题。锁通常用于防止其他线程发生的并发状态更改。

The advantage of using reentrant locks is that you don't have to worry about the possibility of failing due to accidentally acquiring a lock that you already hold. The downside is that you can't assume that nothing you call will change the state of the variables that the lock is designed to protect. However, that's not usually a problem. Locks are generally used to protect against concurrent state changes made by other threads.


所以我不需要考虑死锁?

So I needn't consider deadlocks?

是的。

线程不会死锁(如果锁是可重入的)。但是,如果有其他线程可能锁定您要锁定的对象,则可能会出现死锁。

A thread won't deadlock against itself (if the lock is reentrant). However, you could get a deadlock if there are other threads that might have a lock on the object you are trying to lock.

这篇关于java中的“重入”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆