1

I need my application to submit data to a web service and wait for 90 sec for the response. But if there's a no response in 60 secs I need to redirect the user to a different page and continue to wait for the the response for another 30 secs if it come then process it. I know I need to use thread for this but not sure how to integrate the treads in this case so threads can exchange data between themselves.

Any ideas?? I'm using JSF for UI.

The requirment is follows : The web service will send response in 90 secs (That's the maximum response time for it). But the user will be given a response(A dummy response in case the response does not come within 60 sec) in 60 sec. So even if the user has been given a dummy response (after 60 sec) my application will continue to wait for another 30 sec for the response

4

1 回答 1

1

不太了解JSF,但听起来你想要一个计时器,可能是java.util.Timer。如果在计时器响起之前回答回来,请关闭计时器。如果计时器关闭,请将其重置 30 秒并重定向用户。下次它熄灭时,放弃等待正确答案。

这么多你似乎明白了。但是这里至少有两个交互线程线程。如何沟通?

只需使用实例字段。所有对它们的引用都应该使用synchronized方法或块中的代码来完成。这样做,你应该没事。你必须弄清楚,但我想你会有一个 int timerPhase,这表明计时器没有启动,在前 60 秒,在接下来的 30 秒内,或超时。也是一个布尔值answerReceived,里面有答案,也许还有其他一些。

(过多的同步会减慢您的程序速度。 我认为您不会遇到这个问题。 但如果您这样做,请将同步块拆分,每个字段单独同步,除非它们交互。删除同步并使用volatile关键字。阅读关于多线程。(阅读volatile关键字。)认真思考并行线程如何交互。并为事情变得非常奇怪做好准备。)

于 2012-04-23T17:07:44.533 回答