"non-block-structured locking" 是什么意思?
What does "non-block-structured locking" mean?
我正在阅读 Java 并发实践。在 13.1 Lock 和 ReentrantLock 中,它说:
Why create a new locking mechanism that is so similar to intrinsic locking? Intrinsic locking works fine in most situations but has some functional limitations— it is not possible to interrupt a thread waiting to acquire a lock, or to attempt to acquire a lock without being willing to wait for it forever. Intrinsic locks also must be released in the same block of code in which they are acquired; this simplifies coding and interacts nicely with exception handling, but makes non-block-structured locking disciplines impossible.
"non-block-structured locking"是什么意思?我觉得是说可以在一种方法中锁定,在另一种方法中解锁,比如Lock
,但是内在的必须在获取锁的同一代码块中释放锁。我说得对吗?
但是那本书的中文版把"block"翻译成了“阻塞”。是不是出错了?
块结构锁是指获取和释放锁的模式反映了词法代码结构。具体来说,获取锁的一段代码同时负责释放锁;例如
Lock lock = ...
lock.acquire();
try {
// do stuff
} finally {
lock.release();
}
备选方案 ("non-block-structured locking") 仅表示上述约束不适用。您可以执行一些操作,例如在一种方法中获取锁并在另一种方法中释放它。或者(假设地)甚至将锁传递给另一个线程以释放1。问题是这种代码比上面的例子更难正确,也更难分析。
1 - 小心。如果您在线程之间传递获得的锁,您很容易 运行 进入内存异常或更糟。事实上,我什至不确定它在 Java.
中是否合法
引用文本中提到的 "block structure" 显然是在谈论代码的词汇结构,根据维基百科关于主题 Block (programming). If the Chinese version of Java Concurrency in Practice uses characters that mean "blocking" in the sense of Blocking (programming) 的文章,这是一个误译。
我正在阅读 Java 并发实践。在 13.1 Lock 和 ReentrantLock 中,它说:
Why create a new locking mechanism that is so similar to intrinsic locking? Intrinsic locking works fine in most situations but has some functional limitations— it is not possible to interrupt a thread waiting to acquire a lock, or to attempt to acquire a lock without being willing to wait for it forever. Intrinsic locks also must be released in the same block of code in which they are acquired; this simplifies coding and interacts nicely with exception handling, but makes non-block-structured locking disciplines impossible.
"non-block-structured locking"是什么意思?我觉得是说可以在一种方法中锁定,在另一种方法中解锁,比如Lock
,但是内在的必须在获取锁的同一代码块中释放锁。我说得对吗?
但是那本书的中文版把"block"翻译成了“阻塞”。是不是出错了?
块结构锁是指获取和释放锁的模式反映了词法代码结构。具体来说,获取锁的一段代码同时负责释放锁;例如
Lock lock = ...
lock.acquire();
try {
// do stuff
} finally {
lock.release();
}
备选方案 ("non-block-structured locking") 仅表示上述约束不适用。您可以执行一些操作,例如在一种方法中获取锁并在另一种方法中释放它。或者(假设地)甚至将锁传递给另一个线程以释放1。问题是这种代码比上面的例子更难正确,也更难分析。
1 - 小心。如果您在线程之间传递获得的锁,您很容易 运行 进入内存异常或更糟。事实上,我什至不确定它在 Java.
中是否合法引用文本中提到的 "block structure" 显然是在谈论代码的词汇结构,根据维基百科关于主题 Block (programming). If the Chinese version of Java Concurrency in Practice uses characters that mean "blocking" in the sense of Blocking (programming) 的文章,这是一个误译。