开发者

Should I use two threads which can kill each other?

I would like to have a window with a simple form (radio buttons and so on). Users can make there selections and press a "Submit" button. Additionally to that I would like to set some time limits. In more details, user should see how many se开发者_运维百科conds he/she still have (so, there should be a timer). If the time limit is exceeded, program close the window and submit a "default" selection.

As far as I understand I need to generate 2 threads for that. One will monitor the user activity in the window (which radio button is selected and so on).

Another thread should create a timer which check the remaining time every second and put this information, in some way, into the window.

Moreover, these two thread should be able to kill each other. For example, if the "Submit" button is pressed, the first thread kills the timer. Or if the time is exceeded the second thread (timer) kill the first process.

Is it a good architecture? I need your opinion since I do such kind of things for the first time.


While being somewhat awkward, it is possible for two threads to kill each other.

However, I don't think you need two threads.

You can use a Swing timer (see this tutorial) to run the clock. When you click "Submit" you can stop the timer by invoking its stop() method. The beauty is that Swing timers run on the GUI thread so you don't get the concurrency headache.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜