在不使用java中的static关键字的情况下从回调方法获取变量

时间:2013-11-29 12:37:36

标签: java

类MyApplication观察另一个类并且本身就被观察到了。问题是“run”方法看不到booans replyOne和replyTwo对“update”方法的更新。我希望运行MyApplication类的多个副本,因此不希望使用static关键字。有没有办法让run方法看到booleans的更改而不使用static关键字来修改布尔值?

观察和MyApplication类由另一个类启动,如下所示:

final ServerOne firstServer = new ServerOne();
    MyApplication appOne = new MyApplication();

    // subscribe the observers to the observed - eberybody watches each other
    firstServer.addObserver(appOne);
    appOne.addObserver(firstServer);

    // start the  threads 
    Thread firstServerThread = new Thread(firstServer,"firstServer");
    firstServerThread.start();
    Thread thread3 = new Thread(appOne, "appOne");
    thread3.start();

MyApplication类的代码如下:

public class MyApplication extends Observable implements Observer, Runnable {

    private String resp, response;
    private long timeTaken;
    private boolean replyOne,replyTwo;


    @Override
    public void update(Observable observable, Object arg) {

        timeTaken = (System.nanoTime() - ((Long) arg).longValue()) / 1000;

        if (observable instanceof ServerOne) {
            //process ObservableCLass1 update here

            System.out.println("App Thread " + Thread.currentThread().getName() + " received update from serverOne in " + timeTaken + " microSecs");
            this.replyOne = true;
        } else if (observable instanceof ServerTwo) {

            System.out.println("App Thread " + Thread.currentThread().getName() + " received update from serverTwo in " + timeTaken + " microSecs");
            this.replyTwo = true;
        }

        timeTaken = ((Long) arg).longValue();
        setChanged();

        notifyObservers(timeTaken);
        clearChanged();

    }

    public void run() {

        this.replyOne = false;
        this.replyTwo = false;

        while (!this.replyOne || !this.replyTwo) {
            // wait until the observer has set both the reply flags

            if (this.replyOne) {
                System.out.println("Reply one True ");
                setChanged();
                notifyObservers(new String("app Calling after update from serverOne "));
                this.replyOne = false;
            }

            if (this.replyTwo) {
                System.out.println("Reply two True ");
                setChanged();
                notifyObservers(new String("app Calling after update from serverTwo "));
                this.replyTwo = false;
            }
        }

    }
}

2 个答案:

答案 0 :(得分:0)

带有notifyObserversreplyOne的额外事件类对象的

replyTwo将是传统方式。

public class ReplyEvent {
    public boolean replyOne;
    ...
}


ReplyEvent evt = new ReplyEvent();
evt.message = "...";
evt.replyOne = replyOne;
...
notifyObservers(evt);

答案 1 :(得分:0)

有很多问题。首先,查看您的run方法:

this.replyOne = false;
this.replyTwo = false;

while (!this.replyOne && this.replyTwo) {

首先将两个变量都设置为false,然后在replyTwotrue的情况下输入一个循环,这是不太可能发生的。最有可能的意思是:while (!this.replyOne && !this.replyTwo)

但整个结构看起来很可疑。很明显,这里缺少的等待代码应该在循环中等待至少一个标志变为true,但为什么通知也在循环内? run方法在哪种条件下再次等待,何时退出?

也许你过分简化了。预期的代码可能是这样的:

this.replyOne = false;
this.replyTwo = false;

while(!endCondition) {

    while (!this.replyOne && !this.replyTwo) {
        // wait until the observer has set both the reply flags
    }
    if (this.replyOne) {
        System.out.println("Reply one True ");
        setChanged();
        notifyObservers(new String("app Calling after update from serverOne "));
        this.replyOne = false;
    }

    if (this.replyTwo) {
        System.out.println("Reply two True ");
        setChanged();
        notifyObservers(new String("app Calling after update from serverTwo "));
        this.replyTwo = false;
    }
}

我假设您在注释所说的“等待观察者设置了两个回复标志”的位置放置了正确的线程安全结构。否则,没有内存可见性保证,换句话说,即使另一个线程修改了其中一个变量,这个循环也可能无限运行。