2

Java での並行性に関する入門資料や、Java Swing GUI に固有のマルチスレッド技術を読んでいます。私は現在、自分の状況で使用する最善のアプローチが何であるかについて確信が持てません。私の状況は次のとおりです。

ユーザーがその特定の GUI 画面に留まっている間に、音声認識 API のコードを使用してユーザーの音声を聞くことが機能の 1 つであるプログラムを開発しています。音声認識が検出するすべての単語は、UI の Java Swing テキスト フィールドにリアルタイムで追加されます。検出されたすべての単語がテキスト フィールドに追加されることも非常に重要であるため、ユーザーが終了を選択するまで音声認識スレッドが実行されることが重要です。

私のコードは現在、専用クラスのメソッド内に含まれています。

public class VoiceRecognitionCore 
{

    public void RunVoiceRegonition() throws VoiceRecognitionException
    {
          //Voice recognition code here
    }
}

このスレッドを常に実行するための最も効率的で安全な方法と、UI のテキスト フィールドへのアクセスを許可するにはどうすればよいでしょうか。

お時間をいただきありがとうございます

4

2 に答える 2

3

こちらをご覧になることをお勧めしSwingWorkers ます

docs.oracle.com のページから:

SwingWorker provides a number of communication and control features:

•The SwingWorker subclass can define a method, done, which is automatically invoked on the event dispatch thread when the background task is finished.

•SwingWorker implements java.util.concurrent.Future. This interface allows the background task to provide a return value to the other thread. Other methods in this interface allow cancellation of the background task and discovering whether the background task has finished or been cancelled.

•The background task can provide intermediate results by invoking SwingWorker.publish, causing SwingWorker.process to be invoked from the event dispatch thread.

•The background task can define bound properties. Changes to these properties trigger events, causing event-handling methods to be invoked on the event dispatch thread.

于 2013-01-24T10:25:52.327 に答える
3

考えられる方法の 1 つは、別のスレッドで音声認識を実行し、GUI を更新する必要があるときに を使用しSwingUtilities.invokeLater(Runnable runnable)て GUI を更新することです。

于 2013-01-24T10:19:26.043 に答える