首页 \ 问答 \ 对象内在监视器为java.util.concurrent.Lock(Object intrinsic monitor as java.util.concurrent.Lock)

对象内在监视器为java.util.concurrent.Lock(Object intrinsic monitor as java.util.concurrent.Lock)

我相信能够将对象的监视器用作Lock是非常有用的。 那是 :

synchronized(object) {
    ...
}

相当于:

lock.lock();
try {
    ...
} finally {
    lock.unlock();
}

据我所知,使用Lock接口无法实现这一点,因为同步只发生在块中。 我看到的解决方案是使用额外的方法来丰富java.lang.Object

public Lock asLock/getIntrinsicMonitorObject(); // Name isn't perfect but i'm not asking for that

或类似的东西:

LockSupport.getIntrinsicMonitorObject(Object of);

问题:

  • 是否有一些很好的理由暂时不可能这样做(我认为某些tryLock目前不可能同步而不是一个)

  • 是否有一些使用现有JRE类的解决方案?

  • JSR对此有意义吗?


I believe it could be very useful to be able to use an object's monitor as a Lock. That is :

synchronized(object) {
    ...
}

would be equivalent to :

lock.lock();
try {
    ...
} finally {
    lock.unlock();
}

As far as i understand it, it is not possible to achieve this using Lock interface since synchronization only happens in blocks. A solution i see would be to have java.lang.Object be enriched with an extra method

public Lock asLock/getIntrinsicMonitorObject(); // Name isn't perfect but i'm not asking for that

or something like :

LockSupport.getIntrinsicMonitorObject(Object of);

Questions :

  • Is there some good reasons it isn't possible to do this for now (i consider the fact that some tryLock isn't currently possible with synchronization isn't one)

  • Is there some solution to do it using existing JRE classes ?

  • Would a JSR on this make sense ?


原文:https://stackoverflow.com/questions/22375370
更新时间:2023-09-21 18:09

最满意答案

你正在寻找红/黑树上的分割操作,它采用红/黑树和一些值k并将其分成两个红/黑树,一个所有键大于或等于k,一个全部键小于k。 如果您扩充结构以存储一些额外信息,则可以在O(log n)时间内实现。 在您的情况下,由于您使用的是Java,因此您可以拆分树并丢弃您不关心的树的根,以便垃圾收集器可以处理它。

本文给出了有关如何实现这一点的详细信息,从第9页开始。它是根据结合两棵树的连接 (或连接 )操作实现的,但我认为该说明非常清楚。

希望这可以帮助!


You're looking for the split operation on a red/black tree, which takes the red/black tree and some value k and splits it into two red/black trees, one with all keys greater than or equal to k and one with all keys less than k. This can be implemented in O(log n) time if you augment the structure to store some extra information. In your case, since you're using Java, you can just split the tree and discard the root of the tree you don't care about so that the garbage collector can handle it.

Details on how to implement this are given in this paper, starting on page 9. It's implemented in terms of a catenate (or join) operations which combines two trees, but I think the exposition is pretty clear.

Hope this helps!

相关问答

更多
  • 堆可能具有更好的插入和合并时间。 它实际上取决于堆的类型,但通常它们远不如AVL严格,因为它们不必担心每次操作后的自动平衡。 堆只是保证所有节点在堆中遵循相同的排序方式。 当然有更严格的堆如二进制堆使得插入和合并更加困难,因为排序更重要,但情况并非总是如此。 例如,对于AVL,Fibonacci堆的插入和合并时间将是O(1) vs O(log n) 。 与堆相比,构建完整的AVL也更加困难。 当我们只想快速访问最小和最大项目并且不关心其他元素的完美排序时,我们通常使用堆。 通过快速插入,我们可以快速处理许多 ...
  • 第1部分 - 高度 如starblue所说,高度只是递归。 在伪代码中: height(node) = max(height(node.L), height(node.R)) + 1 现在,高度可以通过两种方式定义。 它可以是从根到该节点的路径中的节点的数量,或者可以是链路的数量。 根据您引用的页面 ,最常见的定义是链接数量。 在这种情况下,完整的伪代码将是: height(node): if node == null: return -1 else: ret ...
  • 您可以按如下所示使用集合中的SortedSet require 'set' s = SortedSet.new([8,2,9,3]) => # 传递参数数组作为参数 You can use SortedSet from set as shown below require 'set' s = SortedSet.new([8,2,9,3]) => # pass argument array as ...
  • 您不应该首先围绕根旋转,而应首先旋转右子树,因为它也是不平衡的。 这个 10 / 8 / \ 7 9 应该旋转并转换为 8 / \ 7 10 / 9 然后树就会 6 \ 8 / \ 7 10 / 9 然后你绕着根旋转 8 / \ 6 10 \ / 7 9 You should not rotat ...
  • 在某些情况下,知情搜索可能有所帮助。 在最坏的情况下,计算成本与您的算法完全相同。 举个例子: int minSumPathOpt(TreeNode* root) { if(root == nullptr) return 0; int sum = -1; std::stack> todo; todo.push(std::make_pair(root, 0)); while(not todo.empty()) { ...
  • 没有。有几种自平衡类型的树木,最受欢迎的是AVL和Red-Black。 如果在这些树中放入相同的数据,生成的树将会有所不同,但却是平衡的。 如果谈到AVL树,我将举一个简单的例子: 2 | 4 | 3 1 4 | 2 5 | 2 4 3 5 | 1 3 | 1 5 所有这些都是平衡的AVL树,可以通过不同的操作顺序创建。 但是如果用完全相同的数据重复相同的操作顺序,结果树应该 ...
  • 你正在寻找红/黑树上的分割操作,它采用红/黑树和一些值k并将其分成两个红/黑树,一个所有键大于或等于k,一个全部键小于k。 如果您扩充结构以存储一些额外信息,则可以在O(log n)时间内实现。 在您的情况下,由于您使用的是Java,因此您可以拆分树并丢弃您不关心的树的根,以便垃圾收集器可以处理它。 本文给出了有关如何实现这一点的详细信息,从第9页开始。它是根据结合两棵树的连接 (或连接 )操作实现的,但我认为该说明非常清楚。 希望这可以帮助! You're looking for the split op ...
  • C ++集(即std::set )通常以红黑树的形式实现 。 他们是自我平衡的。 然而,它的实现,你的建议,该集将变得更像一个列表不会发生,因为该标准使复杂性保证,不能满足一个清单。 C++ sets (i.e. std::set) are usually implemented as red-black trees. They are self balancing. However it is implemented, your suggestion that the set would become m ...
  • 它是最低的上限,意味着它达到了一些n。 请注意,具有根的普通树仅具有定义为0的高度,而不是您可能假设的1。 例如,具有3个元素的完美平衡树具有高度1,具有4个元素的树具有高度2,其为log(4)。 It's the lowest upper bound, meaning it is reached for some n. Note that the trivial tree with the root only has a height defined to be 0, not 1 as you proba ...
  • 一些建议: 1)首先,尝试找到已sorted short list的中间元素(示例中为623),结果为index 2)将短列表分为下半部分(均为小于中间元素的元素)和上半部分(均为大于中间元素的元素)。 3)对于下半部分,我们开始从0搜索到长列表的index ,而对于上半部分,我们开始从index + 1搜索到n (n是长列表的长度) 4)递归执行步骤1的两半。 Some suggestion: 1) First, try to find the middle element (623 in the exa ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。