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

同期から非同期への移行は常に抜本的な設計変更です。結果を待って実際にプルするのではなく、バックグラウンド タスクが結果を適切な場所にプッシュする必要があるからです。最終的に、これは他の Swing リスナーとほとんど同じように見えます。バックグラウンド タスクに渡されるインスタンスに実装する独自のコールバック インターフェイスを作成できます。タスクは、そのインスタンスで「結果準備完了」メソッドを呼び出します。これにより、結果処理コードが呼び出されます。

クラスもありますがSwingWorker、スレッドプールを実装していません。すべてのインスタンスが新しいスレッドを使用します。

于 2012-08-24T09:33:31.153 に答える
0

これは、ある種の非同期呼び出しによく似ています。通常、フレームワークが異なれば、これに対するサポートも異なります

@Async // Servlets/Springs が行います。GUIにもそれが必要です

フレームワークでこれが許可されていない場合、スレッド プールでこれを行うことに問題があるとは思えません。

于 2012-08-24T09:28:41.437 に答える