2011-01-22 20 views
-2

私はThreadをJavaで使用します。 Threadを拡張するクラスを作成しても問題ありませんが、このThreadを呼び出すと、このクラスのインスタンスの数はわかりません。なぜなら、ユーザーはこの番号を入力する必要があるからです。たとえば:Javaでスレッドを使用するにはどうすればよいですか?

multiThread multiThreadInstance = new multiThread(/* number entered from user */); 
multiThreadInstance.start(); 

これは、一度、このスレッドを呼び出しますが、私が書いた場合:

multiThread multiThreadInstance1 = new multiThread(/* number entered from user */) 
multiThreadInstance1.start() 
multiThread multiThreadInstance2 = new multiThread(/* number entered from user */) 
multiThreadInstance2.start() 

これには、同時に二回それを呼び出す、となります。例えば

私はforループに入れた場合、ユーザーが入力した場合3、そしてstart1実行、ときstart1仕上げ、start2実行、ときstart2仕上げ、start3実行されます。私は、スレッドのこれらのインスタンスを同時に実行する必要があります。これどうやってするの?

+0

申し訳ありませんが、私は何かを逃したが、私は何の問題を理解しすぎてよく分からない場合。まず、ユーザーから入力された*番号があなたの質問に重要ですか?あなたの質問にあるすべての例が機能しているようです。あなたのループ実装用のコードを投稿して、うまくいかないか、やり方がわからないものを教えてください。 – gabuzo

答えて

1

これを行うには、Java高水準並行性ユーティリティを使用する必要があります。 countdownlatchesとexecutorを見てください。以下はあなたが望むことをするコードです。私はあなたがjava並行性ユーティリティを読むことをお勧めします。

import java.util.concurrent.*; 

public class ConcurrentTimer { 
    private ConcurrentTimer() { } // Noninstantiable 

    public static long time(Executor executor, int concurrency, 
      final Runnable action) throws InterruptedException { 
     final CountDownLatch ready = new CountDownLatch(concurrency); 
     final CountDownLatch start = new CountDownLatch(1); 
     final CountDownLatch done = new CountDownLatch(concurrency); 

     for (int i = 0; i < concurrency; i++) { 
      executor.execute(new Runnable() { 
       public void run() { 
        ready.countDown(); // Tell timer we're ready 
        try { 
         start.await(); // Wait till peers are ready 
         action.run(); 
        } catch (InterruptedException e) { 
         Thread.currentThread().interrupt(); 
        } finally { 
         done.countDown(); // Tell timer we're done 
        } 
       } 
      }); 
     } 

     ready.await();  // Wait for all workers to be ready 
     long startNanos = System.nanoTime(); 
     start.countDown(); // And they're off! 
     done.await();  // Wait for all workers to finish 
     return System.nanoTime() - startNanos; 
    } 
} 

上方に設けられたコード例の実行可能なバージョン:(編集後)

import java.util.concurrent.*; 

public class ConcurrentTimer { 

    public static void main(String[] args) { 


     try { 
      Runnable action = new Runnable() { 
        public void run() { 

         System.out.println("Thread Running"); 

        } 
       }; 

      time (3, action); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 


    private ConcurrentTimer() { } // Noninstantiable 

    public static long time(int concurrency, 
      final Runnable action) throws InterruptedException { 

     Executor executor = Executors.newFixedThreadPool(concurrency); 


     final CountDownLatch ready = new CountDownLatch(concurrency); 
     final CountDownLatch start = new CountDownLatch(1); 
     final CountDownLatch done = new CountDownLatch(concurrency); 

     for (int i = 0; i < concurrency; i++) { 
      executor.execute(new Runnable() { 
       public void run() { 
        ready.countDown(); // Tell timer we're ready 
        try { 
         start.await(); // Wait till peers are ready 
         action.run(); 
        } catch (InterruptedException e) { 
         Thread.currentThread().interrupt(); 
        } finally { 
         done.countDown(); // Tell timer we're done 
        } 
       } 
      }); 
     } 

     ready.await();  // Wait for all workers to be ready 
     long startNanos = System.nanoTime(); 
     start.countDown(); // And they're off! 
     done.await();  // Wait for all workers to finish 
     return System.nanoTime() - startNanos; 
    } 
} 
+0

もちろん、それは参考にされました。私はメインと一緒に実行可能なコードを追加しました。 –

+0

本当に正しい答えです。 –

2

start()メソッドThreadに誤ってオーバーライドしている可能性があります。オーバーライドする方法がrun()であることを確認してください。

独自の実装でstart()メソッドをオーバーライドすると、Threadクラスの「マジックを削除する」ことができます。魔法はrun()メソッドの実行を新しいスレッドで開始するstart()にあるので、独自のコードをrun()に入れてください。

+0

私は自分のコードをrunメソッドに入れました。私はmultiThreadInstance.start()メソッドが動作していると言っています –

+0

runメソッドで同期がありますか?例えば ​​'wait '/' notify'のようなものです。 – aioobe

+0

いいえ、私の実行方法に同期がありません。私は睡眠方法しか持っていません。 –

関連する問題