"ReentrantLock"的含义是什么?在Java中? [英] What is the meaning of "ReentrantLock" in Java?

查看:107
本文介绍了"ReentrantLock"的含义是什么?在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?

谢谢,这似乎意味着:在线程中,如果调用过程函数doB的进程函数doAdoB也需要锁lockA时得到锁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"的锁定

使用可重入锁/锁定机制,获取相同锁的尝试将成功,并将增加属于该锁的内部计数器.仅当当前锁的持有者两次释放锁时,锁才会被释放.

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.

使用可重入锁的优点是,您不必担心由于意外获取已经持有的锁而导致失败的可能性.不利的一面是,您不能假设调用任何内容都不会改变锁旨在保护的变量的状态.但是,通常这不是问题.锁通常用于防止 other 线程进行的并发状态更改.

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.

这篇关于"ReentrantLock"的含义是什么?在Java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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