2014-04-12 41 views
0

我有一個實現Runnable接口(用於可讀性,我已經簡化它)計算類:爲什麼在可觀察對象作爲新線程運行時沒有通知觀察者?

public class ColorReduction extends Observable implements Runnable { 

    public ColorReduction(Layer inputLayer, Layer outputLayer) { 
     this.init(); 
    } 

    private void init() { 
     addObserver(new ActionObserver()); 
    } 

    public void run(){ 

     notifyObservers(EAction.COLOR_REDUCTION_START); 
     setChanged(); 

     // some computation stuff..... 

     notifyObservers(EAction.COLOR_REDUCTION_FINISH); 
     setChanged(); 

    } 

} 

,並在控制器我運行這個類作爲一個新的線程:

ColorReduction cr = new ColorReduction(model.getInputLayer(), model.getOutputLayer()); 

      Thread t = new Thread(cr); 
      t.start(); 

問題:觀察員(ActionObserver)僅在顏色縮小階段結束時(COLOR_REDUCTION_FINISH)通知,而不是在本階段開始時(COLOR_REDUCTION_START)。

我的代碼有什麼問題?

回答

2

您需要先調用setChanged():

this.setChanged(); 
this.notifyObservers(EAction.YOUR_ENUM_VALUE); 
相關問題