首页 \ 问答 \ 使用java ConcurrentSkipListSet添加方法时线程卡住了(Thread stuck when use java ConcurrentSkipListSet add method)

使用java ConcurrentSkipListSet添加方法时线程卡住了(Thread stuck when use java ConcurrentSkipListSet add method)

我正在使用ConcurrentSkipListSet集合来处理并发运算符。 我发现它有时会卡住,用这段代码重现:

import java.util.Comparator
import java.util.concurrent._

object SetDeadLock extends App {
  private val tasks = new ConcurrentSkipListSet[Task](new Comparator[Task](){
    override def compare(o1: Task, o2: Task): Int = {
      val compare = (o1.systemTime - o2.systemTime).toInt
      if (compare == 0) 1 else compare  //distinct same time task
    }
  })

  for(i <- 1 to 20) {
    tasks.add(Task())
    println(s"added - $i")
  }

  case class Task() {
    val systemTime = System.currentTimeMillis()
  }
}

产量

added - 1
added - 2

它或许卡在别人身上,另外,比较器自定义排序数据方法尤其是它们相同(因为Set不支持相同的元素)而且所有Task都是新实例,它不应该与其他实例冲突。

使用jstack cmd,主线程卡住了

at java.util.concurrent.ConcurrentSkipListMap.findPredecessor(ConcurrentSkipListMap.java:685)

这是一个错误还是我误导了一些原则?

感谢任何帮助或建议。

UPDATE
我现在尝试修改if (compare == 0) 1 else compare if (compare == 0) -1 else compare ,令人惊讶,我运作良好!

有没有人能澄清它是如何工作的? 源代码对我来说很难(我认为很多人都同意我的观点),毕竟,jdk为机器执行速度做了很多工作而不是代码阅读器。

最后
为了避免尴尬的情况,只需对comparator Set的语义进行一些不同的因素,例如附加一个随机值。但我认为它会更好地找到另一个真正合适的集合。

几天前,我发现在systemTime等于时使用hashCode作为第二次验证是个好主意。 希望有用〜


I'm using ConcurrentSkipListSet collection to deal with concurrent operator. I find it will stuck sometimes, reproduct with this code:

import java.util.Comparator
import java.util.concurrent._

object SetDeadLock extends App {
  private val tasks = new ConcurrentSkipListSet[Task](new Comparator[Task](){
    override def compare(o1: Task, o2: Task): Int = {
      val compare = (o1.systemTime - o2.systemTime).toInt
      if (compare == 0) 1 else compare  //distinct same time task
    }
  })

  for(i <- 1 to 20) {
    tasks.add(Task())
    println(s"added - $i")
  }

  case class Task() {
    val systemTime = System.currentTimeMillis()
  }
}

Output

added - 1
added - 2

It perhaps stuck at others, besides, the comparator custom sort data method especially they are same (because Set not support same element) and all Task is new instance, it shouldn't be conflict with others.

with jstack cmd, the main thread stuck

at java.util.concurrent.ConcurrentSkipListMap.findPredecessor(ConcurrentSkipListMap.java:685)

does it was a bug or I just misleading some principle?

Thanks any help or advise.

UPDATE
I just now try modify if (compare == 0) 1 else compare to if (compare == 0) -1 else compare, surprising, I works well!

Does anyone could clarify how it work? Source code its difficult for me(and I think many people agree with me), after all, jdk do many works for machine execute speed not for code reader.

Finally
To avoid the awkward situation just make some different factor to comparator which match Set's semantics, such as appends a random value.But I think its will be better find another real suitable collection.

With a few days ago, I find a good idea to use hashCode as second verify when systemTime equals. hope helpful~


原文:https://stackoverflow.com/questions/37558703
更新时间:2023-04-28 17:04

最满意答案

您需要指定一个配置映射,该映射排除可选依赖项和隐式依赖项(如source和javadoc文件)。

在常春藤文件中,您将声明依赖项如下:

<dependency org="myorg" name="mymodule" rev="1.0" conf="default"/>

另一种选择是将此依赖关系映射声明为项目的默认值。

<dependencies defaultconfmapping="default">
  <dependency ..
  <dependency ..

请参阅以下有关如何排除源以及常春藤如何翻译Maven存储库的SO问题:

总之,Maven模块默认只有一个arefact。 可以存储更多(如源和Javadoc),但没有列出它们的模块元数据。 这就是为什么Ivy执行HTTP请求以查看伪像是否存在的原因。 更改配置映射设置应该有助于指导常春藤做正确的事:-)

PS

我认为这可以通过将“addDependencyConfiguration”方法调用到DefaultDependencyDescriptor对象来完成。


You need to specify a configuration mapping that excludes both optional dependencies and implicit dependencies like source and javadoc files.

In an ivy file you'd declare the dependency as follows:

<dependency org="myorg" name="mymodule" rev="1.0" conf="default"/>

Another option is to declare this dependency mapping as the default for your project.

<dependencies defaultconfmapping="default">
  <dependency ..
  <dependency ..

See the following SO questions on how to exclude sources and how ivy translates Maven repositories:

In conclusion Maven modules by default only have a single arefact. More can be stored (like sources and Javadocs) but there is no module metadata listing them. This is why Ivy performs a HTTP request to see if the artefacts exist or not. Changing the configuration mapping settings should help guide ivy to do the right thing :-)

PS

I think this can be done by called the "addDependencyConfiguration" method to the DefaultDependencyDescriptor object.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取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的基本操作命令。。。