同步问题:
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/