2017-02-12 14 views
0

join()とisAlive関数を使用します。私は4つのスレッドを作成し、それらをコンストラクタで初期化しました。私はstartメソッドを使ってスレッドを開始しました。 run()を呼び出す必要がありますが、runメソッドは実行されていません。私は間違ったことをしていますか?教えてください。前もって感謝します。run()メソッドがstart()を使用して実行されていません

class MultiThreads implements Runnable { 

    String name; 
    Thread t; 

    MultiThreads(String tname) { 
     name=tname; 
     t=new Thread(this.name); 
     System.out.println("Thread name: " + t); 
     t.start();    //executing run() 
    } 

    public void run(){ 

     try{ 
     for(int i=1;i<11;i++){ 
      System.out.println("Thread-"+name+ ": " + i); 
      t.sleep(500);   
      } 
     }catch(Exception ie){ 
      System.out.println("An error has occurred"); 
     } 
    } 
} 

public class JoinAlive { 

    public static void main(String args[]) { 

     //Creating New Threads by calling constructor. 

     MultiThreads t1=new MultiThreads("One");`` 
     MultiThreads t2=new MultiThreads("Two"); 
     MultiThreads t3=new MultiThreads("Three"); 
     MultiThreads t4=new MultiThreads("Four"); 

     System.out.println(); 

     System.out.println("Thread-One active: " + t1.t.isAlive()); 
     System.out.println("Thread-Two active: " + t2.t.isAlive()); 
     System.out.println("Thread-Three active: " + t3.t.isAlive()); 
     System.out.println("Thread-Four active: " + t4.t.isAlive()); 

     try{ 
      System.out.println(); 
      System.out.println(" Waiting for One"); 
      t1.t.join(); 
      System.out.println(" Waiting for Two"); 
      t2.t.join(); 
     }catch(InterruptedException ie){ 
      System.out.println("An error occurred"); 
      } 

      System.out.println(); 

      System.out.println("Thread-One active: " + t1.t.isAlive()); 
      System.out.println("Thread-Two active: " + t2.t.isAlive()); 
      System.out.println("Thread-Three active: " + t3.t.isAlive()); 
      System.out.println("Thread-Four active: " + t4.t.isAlive()); 
    } 
} 

答えて

2

t=new Thread(this.name);が問題です。
スレッドに名前を付けますが、関連するターゲットのRunnableインスタンスを指定していません。このように

public Thread(Runnable target, String name) 


はちょうどこのコンストラクタを使用

t=new Thread(this,this.name); 
0

あなたのコード内でThreadオブジェクトの作成は、目標のないスレッドです。

あなたはこれを使用している:

t=new Thread(this.name); 

あなたはこれを使用してスレッドを初期化する必要があります。それはあなたのスレッドの開始方法にあなたのRunnableのrunメソッドをリンクします

t=new Thread(this, this.name); 

を。


0

使用この編集したコード `クラスのマルチスレッドはRunnableを{

String name; 
Thread t; 

MultiThreads(String tname) { 
    name=tname; 
    t=new Thread(this.name); 
    System.out.println("Thread name: " + t); 
    t.start();    //executing run() 
} 

public void run(){ 

    try{ 
    for(int i=1;i<11;i++){ 
     System.out.println("Thread-"+name+ ": " + i); 
     t.sleep(500);   
     } 
    }catch(Exception ie){ 
     System.out.println("An error has occurred"); 
    } 
} 

}

パブリッククラスJoinAliveあなたがにThreadクラスを拡張するか必要がある{

public static void main(String args[]) { 

    //Creating New Threads by calling constructor. 

    Thread t1=new Thread(new MultiThreads("One")); 
    Thread t2=new Thread(new MultiThreads("Two")); 
    Thread t3=new Thread(new MultiThreads("Three")); 
    Thread t4=new Thread(new MultiThreads("Four")); 

    System.out.println(); 

    System.out.println("Thread-One active: " + t1.isAlive()); 
    System.out.println("Thread-Two active: " + t2.isAlive()); 
    System.out.println("Thread-Three active: " + t3.isAlive()); 
    System.out.println("Thread-Four active: " + t4.isAlive()); 

    try{ 
     System.out.println(); 
     System.out.println(" Waiting for One"); 
     t1.join(); 
     System.out.println(" Waiting for Two"); 
     t2.join(); 
    }catch(InterruptedException ie){ 
     System.out.println("An error occurred"); 
     } 

     System.out.println(); 

     System.out.println("Thread-One active: " + t1.isAlive()); 
     System.out.println("Thread-Two active: " + t2.isAlive()); 
     System.out.println("Thread-Three active: " + t3.isAlive()); 
     System.out.println("Thread-Four active: " + t4.isAlive()); 
} 

}`

+0

を実装thread.start()が呼び出されたときにrunメソッドが呼び出されることを確認するか、またはuがRunnable Yoを実装する場合あなたのクラスをThreadクラスのコンストラクタにフィードする必要があります。 – user121290

関連する問題