我正在同步2个线程使用的ArrayList。通知和等待模式。通知线程,但arrayList包含旧值虽然另一个线程打印新值。
TH1:
synchronized( pairsBarBuffers ) {
try {
pairsBarBuffers.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Th2细胞:
synchronized (pairs15MBarBuffers) {
if( countSamples == 0 ){
return;
}
updateBarBuffer( pairs15MBarBuffers , countSamples );
pairs15MBarBuffers.notify();
countSamples = 0;
}
有两种显式同步方法:
Using Collections.synchronizedList() method
Using thread-safe variant of ArrayList: CopyOnWriteArrayList