ObservableのnotifyObserversメソッドで、なぜ符号器はarrLocal = obs.toArray();
を使用しますか? コーダーがベクターを直接反復処理しないのはなぜですか?おかげObservable snapshot observer vector
public void notifyObservers(Object arg) {
Object[] arrLocal;
synchronized (this) {
/* We don't want the Observer doing callbacks into
* arbitrary code while holding its own Monitor.
* The code where we extract each Observable from
* the Vector and store the state of the Observer
* needs synchronization, but notifying observers
* does not (should not). The worst result of any
* potential race-condition here is that:
* 1) a newly-added Observer will miss a
* notification in progress
* 2) a recently unregistered Observer will be
* wrongly notified when it doesn't care
*/
if (!changed)
return;
arrLocal = obs.toArray();
clearChanged();
}
for (int i = arrLocal.length-1; i>=0; i--)
((Observer)arrLocal[i]).update(this, arg);
}
Observerがコールバック内に別のリスナーを追加しようとする(またはリスナーとして自身を削除する)場合、オプション1はデッドロックを作成します。 – sje397
私はスレッドがデッドロックすることはできません(Observersは同じスレッドで呼び出されます)。もちろん、スレッドが何らかの形で関与している場合は、それが起こる可能性があります。 – Thilo
「Observerが別のリスナーを追加しようとした場合」でもオプション1はConcurrentModificationExceptionを引き起こす可能性があります。コピーはとにかく助言されます。 – Thilo