0

I have an application that I'm trying to restructure to use the MVC architecture. The GUI kicks off several long running function calls that would block the interface if I don't detach them from the GUI thread.

Now I was wondering whether wrapping the objects of the controller that have such long running functions into another version that just detaches the current call to a thread pool would be a good idea? Is there any pattern for this sort of problem?

EDIT Maybe I should provide a sample. Currently my code is littered with the following:

env.getScheduledExecutor().execute(new Runnable() {
  public void run() {
    backend.someLongRunningMethod();
  }
});

Now I' wondering whether I should wrap the Backend into something like a BackendFacade class that will just wrap any function call into a runnable and schedule it to the executor.

It is worth noticing that most times the methods return void or I don't care anyway. In the rare cases that I need to get the value back I'll just use Future. It should not really be a huge change since I already decouple threads at some point but I'd like to consolidate how it is done across the whole project.

4

2 回答 2

0

这听起来很像某种异步调用。通常不同的框架对此有不同的支持

@Async // Servlets/Springs 可以。GUI也应该有

如果您的框架不允许这样做,那么我看不出让线程池这样做有什么问题。

于 2012-08-24T09:28:41.437 回答
0

从同步到异步始终是一个彻底的设计更改,因为现在后台任务需要将结果送到适当的位置,而不是等待结果并实际上拉取它。最后,这看起来很像任何其他 Swing Listener。您可以发明自己的回调接口,并在传递给后台任务的实例中实现。该任务将调用该实例上的“结果就绪”方法,该方法将调用结果处理代码。

还有这个SwingWorker类,但它没有实现线程池。每个实例都将使用一个新线程。

于 2012-08-24T09:33:31.153 回答