gpt4 book ai didi

Java HashSet 同步查询

转载 作者:行者123 更新时间:2023-12-01 15:19:30 28 4
gpt4 key购买 nike

同步问题:

Set s = Collections.synchronizedSet(new HashSet());
private Object monitor_ = new Object();

//Set has element added in another routine...

//Called by some thread1, thread2, ...
TimerTask run(){ //method which executes every 1 min
synchronized(monitor_) {
s.isEmpty()
// ...
// waits for 30 seconds
// JNI call
// ...
}
}

//Called by cleanup thread
removecall()
{
synchronized( monitor_ ) {
s.remove( something );
}
}

问题:当 TimerTask run 方法正在执行时,清理线程必须等待。任何有效的方法无需等待即可处理这种情况。例如可重入锁

最佳答案

并发集可能是解决方案:CopyOnWriteArraySet或 Collections.newSetFromMap( ConcurrentHashMap ),但鉴于我对您的问题的了解,很难说。

关于Java HashSet 同步查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11155714/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com