4

マルチスレッド用のJavaRunnableインターフェイスを実装しています。いくつn かのスレッドがあります。それぞれのスレッドには独自の寿命があります。すべてのスレッドの寿命が切れるまで待ちたいです。以下がその場合だとしましょう

for(int i = 0; i< k;i++){
 Thread thread1 = new Thread(new xyz())
 Thread thread2 = new Thread(new abc())
 Thread thread3 = new Thread(new mno())
 thread1.start();
 thread2.start();
 thread3.start();
}

私はそれを同期させるために以下を行っています。それが正しいかどうかはわかりません。どうすればいいですか?また、スレッド化されたプログラムが正しく機能しているかどうかを確認する方法はありますか?

          if(thread2.isAlive())
                try {
                    thread2.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            if(thread1.isAlive())
                    try {
                        thread1.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
            if(thread3.isAlive())
                try {
                        thread3.join();
                } catch (InterruptedException e) {
                e.printStackTrace();
                    }   
4

5 に答える 5

8

RunnablesをExecutorServiceに追加し、 shutdown / awaitTerminationを呼び出すことができます。これは、すべてのタスクが完了すると返されます。javadocにはいくつかの例があります。要約すると、次のように記述します。

ExecutorService executor = Executors.newFixedThreadPool(3);

executor.submit(runnable1);
executor.submit(runnable2);
executor.submit(runnable3);

executor.shutdown();
boolean allRunnableAreDone = executor.awaitTermination(60, TimeUnit.SECONDS);

// This line is reached once all runnables have finished their job
// or the 60 second timeout has expired
于 2012-06-28T15:48:06.333 に答える
4

@assyliasからのExecutorService回答は良いものですが、ここにについての詳細がありますjoin()

isAlive()前にテストする必要はありませんjoin()Thread.join()コードはすでにそれを行っています。そこにあるコードの抜粋は次のとおりです。

while (isAlive()) {
    wait(0);
}

したがって、必要なのはスレッドに参加することだけです。

   try {
       thread2.join();
       thread1.join();
       thread3.join();
   } catch (InterruptedException e) {
       // always a good pattern
       Thread.currentThread().interrupt();
       e.printStackTrace();
   }
于 2012-06-28T15:52:49.637 に答える
2

簡単な解決策として、すべてのスレッドをリストに入れ、開始後に結合を呼び出すことができます。

List<Thread> threads = new ArrayList<>();
for(int i = 0; i< k;i++)
{
  //create threads
  Thread thread1 = new Thread(new xyz());
  Thread thread2 = new Thread(new abc());
  Thread thread3 = new Thread(new mno());

  //store threads
  threads.add(thread1);
  threads.add(thread2);
  threads.add(thread3);

  //start threads
  thread1.start();
  thread2.start();
  thread3.start();
}

//join all threads
for(Thread t : threads)
   t.join();

//You are here after all threads have terminated
于 2012-06-28T15:51:12.583 に答える
1

Javaには、 java.util.concurrentでこの種のことを行うメカニズムが含まれています。

あなたの場合、おそらくCountDownLatchまたはExecutorServiceが必要です

于 2012-06-28T15:45:17.187 に答える
1

メソッドはjoin()スレッドではなく、ロックオブジェクトに参加します。待機中のスレッドは呼び出す必要がlockObject.join()あり、作業中のスレッドはlockObject.notify()それが完了したときに呼び出す必要があります。待機中のスレッドに通知され、作業を続行できます。synchronizeこれらの呼び出しの周りにもブロックが必要です。

また、前述のアシリアのようなエグゼキュータをお勧めします。この動作を自分で実装するよりもはるかに簡単です。

于 2012-06-28T15:51:27.067 に答える