問題是:類C控制類B是否讓B再次將A添加到queueOfA,但是我怎樣才能確保當A通知C,並在B的queueOfA變空之前立即更新,因爲B類運行速度非常快,可能會刪除所有A,因此在C更新之前變爲空queueOfA,這可能會導致C的run方法結束。java線程同步問題,如何實現可觀察線程
請幫我一把!
class A extends Observable implements Runnable{
//...
void run(){
//...
if (goBackToTheQueueAgain == true){
setChanged();
notifyObservers();//notify C that to let B add itself back
}
//...
}
class B extends implements Runnable{
Queue<A> queueOfA;// initialy, queueOfA contains several A's
//...
void run(){
//...
A retrieved = queueOFA.remove();
Thread aThread = new Thread(retrieved);
aThread.start();
//...
}
}
class C implements Observer, Runnable {
B b; //initially class C contains an instance of B
//...
void update(Observable o, Object arg){
if(o instanceof A){
A a = (A) o;
b.queueOfA.add(a);
}
}
//...
void run(){
//...
Thread bThread = new Thread(b);
bThread.start();
while (b.queueOfA.isEmpty() == false){
// if queueOfA is not empty loop for ever
}
//..
}
}
哇,這是一些句子!我想你說,呃,一口氣! – mdma 2010-07-02 18:58:19
深呼吸:) – 2010-07-02 19:01:51
我有一個可怕的英語表達風格。 – Rn2dy 2010-07-02 19:24:27