开发者

Java Swing long Thread execution

开发者 https://www.devze.com 2022-12-20 13:11 出处:网络
I have written a framework that does calculations that take a long time to execute (about 15 minutes). I now want to write an interface in Swing which will gather the data from the database and execut

I have written a framework that does calculations that take a long time to execute (about 15 minutes). I now want to write an interface in Swing which will gather the data from the database and execute this calculations.

I just wondered now how to do that best practice? If I do time intensive computations in the event thread the whole interface freezes until the code is finished.

I started to think that I will create an Object which can handle all the calculation task, but what do I do with the state informati开发者_JAVA技巧ons about progress and status during the execution. I also have several actions that can be executed, e.g. different calculations that need different data. Do have to write an object for each action? If I separate the view and the calculation I have to exchange the data - what is the best practice for that?

Thanks and best regards

Marco


Take a look at SwingWorker (for Java 6, but there's a library version you can get for Java 5). With SwingWorker, you run your time-intensive method within the doInBackground() method and the done() method is called on the Event Queue when it finishes, allowing you to update the GUI. It also provides a means to notify the GUI of progress during the task, if possible.


Read the section from the Swing tutorial on Concurrency for an example on using a SwingWorker.

0

精彩评论

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