问题是:类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