ベクトル オブジェクトに値を設定する 1 つのスレッドと、そこから値を定期的にフェッチして定期的にクリアする別のスレッドがあります。
ベクトルにアクセスしているいずれかのスレッドが一時停止し、もう一方のスレッドがアクセスしている必要があります。wait/notify/notifyAll キーワードを使用する必要がありますか?
private Vector<Long> _recordIdsSent;
# Called by main thread (before thread A and thread B are started)
public ConstructorOfThisClass() {
_recordIdsSent = new Vector<Long>();
// Starts thread A & B
}
# Called by thread A
public void addSentRecordIds( List<Long> ids ) {
synchronized (_recordIdsSent) {
_recordIdsSent.addAll( ids );
}
}
# Called by thread B
public void deleteRecords()
{
List<Long> ids;
synchronized (_recordIdsSent) {
ids = (List<Long>) _recordIdsSent.clone();
_recordIdsSent.clear();
}
// Delete the records matching ids....
}
注: 削除操作には時間がかかる可能性があるため、_recordIdsSent ベクターを複製します。
[編集] synchronized キーワードをメソッド シグネチャから変数 _recordIdsSent に移動しました