同步问题:
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 );
}
}
Problem:
当 TimerTask 运行方法正在执行时,清理线程必须等待。无需等待即可处理这种情况的任何有效方法。例如重入锁