2017-02-12 21 views
0

我想只使用join()和isAlive函數。我創建了4個線程並在構造函數中初始化它們。我使用start方法啓動線程。它應該調用run(),但run方法沒有執行。難道我做錯了什麼。請告訴我。先謝謝你。運行()方法不執行使用開始()

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); 

你應該這樣初始化你的線程:

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

它會連接您的Runnable的run方法的線程的啓動方法。


希望這有助於!

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 {

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類確保在調用thread.start()時調用run方法,或者如果你實現了Runnable Yo你需要將你的類餵給Thread類的構造函數。 – user121290