狠批JDK的Observer接口!!!(言重了....^_^)

Oberver模式常被“標榜”爲快速響應Client的變化,及時的將message分發給訂閱者,kao ...

其實,默認的JDK API根本就滿足不了所謂的快速響應,甚至不如傳統的過程式的執行方式:(

來看看JDK的Oberver都做了些什麼...

這是Observable類的一段通知Observer的代碼

if (!changed)
                return;
            arrLocal = obs.toArray();
            clearChanged();
        }

        for (int i = arrLocal.length-1; i>=0; i--)
            ((Observer)arrLocal[i]).update(this, arg);
    }

 

這個Update方法通知註冊的觀察者,若前一個觀察者將耗時很長時,第二的則需要等待.....汗....

仔細看了JDK的說明

The order in which notifications will be delivered is unspecified. The default implementation provided in the Observerable class will notify Observers in the order in which they registered interest, but subclasses may change this order, use no guaranteed order, deliver notifications on separate threads, or may guarantee that their subclass follows this order, as they choose.

說的很明白了,我們只有自己去開多線程去解決這個問題........

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章