开发者

Return from await() from other method

I’m newbie in Play! and I have one question about asynchronous programming in HTTP. I have a piece of code like this:

public void someMethod() {
    for (int i = 0; i < 100; i++) {
        doSomething();
        await(someTime);
    }
}

This method is invoked by user by sending GET/POST request. It does some computations (doSomething()) and after that it waits some time. But: the user开发者_JAVA百科 has to have ability to “return” from await(someTime) and the loop should continue next iteration without waiting all the “someTime” time.

The example code:

public void nextAwait() {
    continueАForLoop();
}

The user invokes nextAwait() method by GET/POST. If it is invoked, the loop will continue and doSomething() will be (has to be) invoked immediately!

So, is it possible in Play?

Thanks in advance for answers :)


The simple answer to this, is to wait for a shorter period of time, then check some value for user interaction, and then continue waiting.

for example, let's assume your total wait time is 10 seconds

public void someMethod() {
    for (int i = 0; i < 100; i++) {
        doSomething();
        for (int j=0; j<10; j++) {
            if (!userInterrupt) await("1s");
        }
    }
}

So, this breaks your wait down to 1 second chunks, and checks a value to see if the user has interrupted the wait. It means that the user will wait a maximum of 1 second before the processing is released.


I don't know if it works this way but you could try something like that: (You have a field monitor = new Object() somewhere)

synchronized ( this.monitor ) {
  this.monitor.wait( someTime );
}

and in the other method you call:

synchronized ( monitor ) {
  this.monitor.notifyAll();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜