2016-05-12 5 views
1
// Using join() to wait for threads to finish. 
class NewThread implements Runnable { 
    String name; // name of thread 
    Thread t; 
    NewThread(String threadname) { 
     name = threadname; 
     t = new Thread(this, name); 
     System.out.println("New thread: " + t); 
     t.start(); // Start the thread 
    } 
    // This is the entry point for thread. 
    public void run() { 
     try { 
      for (int i = 5; i > 0; i--) { 
       System.out.println(name + ": " + i); 
       Thread.sleep(1000); 
      } 
     } catch (InterruptedException e) { 
      System.out.println(name + " interrupted."); 
     } 
     System.out.println(name + " exiting."); 
    } 
} 
class DemoJoin { 
    public static void main(String args[]) { 
     NewThread ob1 = new NewThread("One"); 
     NewThread ob2 = new NewThread("Two"); 
     NewThread ob3 = new NewThread("Three"); 

     System.out.println("Thread One is alive: " + ob1.t.isAlive()); 
     System.out.println("Thread Two is alive: " + ob2.t.isAlive()); 
     System.out.println("Thread Three is alive: " + ob3.t.isAlive()); 
     // wait for threads to finish 
     try { 
      System.out.println("Waiting for threads to finish."); 
      ob1.t.join(); 
      ob2.t.join(); 
      ob3.t.join(); 
     } catch (InterruptedException e) { 
      System.out.println("Main thread Interrupted"); 
     } 
     System.out.println("Thread One is alive: " + ob1.t.isAlive()); 
     System.out.println("Thread Two is alive: " + ob2.t.isAlive()); 
     System.out.println("Thread Three is alive: " + ob3.t.isAlive()); 
     System.out.println("Main thread exiting."); 
    } 
} 

こんにちは、そこにあります。私はこの完全なJavaリファレンス - 第9版からのコードの平和に問題があります。私はこのコードを実行するたびに異なる出力を得ます。join()メソッドとThe Complete Java Reference第9版

New thread: Thread[One,5,main] 
New thread: Thread[Two,5,main] 
New thread: Thread[Three,5,main] 
One: 5 
Thread One is alive: true 
Thread Two is alive: true 
Thread Three is alive: true 
Waiting for threads to finish. 
Two: 5 
Three: 5 
One: 4 
Three: 4 
Two: 4 
One: 3 
Two: 3 
Three: 3 
One: 2 
Two: 2 
Three: 2 
One: 1 
Three: 1 
Two: 1 
One exiting. 
Two exiting. 
Three exiting. 
Thread One is alive: false 
Thread Two is alive: false 
Thread Three is alive: false 
Main thread exiting. 

本の出力は次のとおりです:

New thread: Thread[One,5,main] 
New thread: Thread[Two,5,main] 
New thread: Thread[Three,5,main] 
Thread One is alive: true 
Thread Two is alive: true 
Thread Three is alive: true 
Waiting for threads to finish. 
One: 5 
Two: 5 
Three: 5 
One: 4 
Two: 4 
Three: 4 
One: 3 
Two: 3 
Three: 3 
One: 2 
Two: 2 
Three: 2 
One: 1 
Two: 1 
Three: 1 
Two exiting. 
Three exiting. 
One exiting. 
Thread One is alive: false 
Thread Two is alive: false 
Thread Three is alive: false 
Main thread exiting. 

さて、私は、スレッドがどのように動作するかを理解しますが、私の新しいスレッドが並行で開始しない理由私はかなり理解していない はここで例です私の主なスレッドと一緒に誰でも助けてくれますか? Ty。

EDIT:join()、sleep()または他の親しみのあるメソッドまでメインスレッドを実行し、そのメソッドが「発生する」と、Thread :: runningはReadyキューから次のスレッドを実行する必要があります?

EDIT:このコード:

// Create a second thread. 
class NewThread implements Runnable { 
Thread t; 
NewThread() { 
// Create a new, second thread 
t = new Thread(this, "Demo Thread"); 
System.out.println("Child thread: " + t); 
t.start(); // Start the thread 
} 
// This is the entry point for the second thread. 
public void run() { 
try { 
for(int i = 5; i > 0; i--) { 
System.out.println("Child Thread: " + i); 
Thread.sleep(500); 
} 
} catch (InterruptedException e) { 
System.out.println("Child interrupted."); 
} 
System.out.println("Exiting child thread."); 
} 
} 

class ThreadDemo { 
public static void main(String args[ ]) { 
new NewThread(); // create a new thread 
try { 
for(int i = 5; i > 0; i--) { 
System.out.println("Main Thread: " + i); 
Thread.sleep(1000); 
} 
} catch (InterruptedException e) { 
System.out.println("Main thread interrupted."); 
} 
System.out.println("Main thread exiting."); 
} 
} 

は常に同じに動作します。どうして?それは)(同じものだが、第二のスレッドが開始された - コンストラクタで編、まだこのコードは同じ結果を毎回与える:

Child thread: Thread[Demo Thread,5,main] 
Main Thread: 5 
Child Thread: 5 
Child Thread: 4 
Main Thread: 4 
Child Thread: 3 
Child Thread: 2 
Main Thread: 3 
Child Thread: 1 
Exiting child thread. 
Main Thread: 2 
Main Thread: 1 
Main thread exiting. 
+1

これは、 'Thread'の' start() 'メソッドと同じです。同時に起動します。この例では、 'NewThread()'コンストラクタで 'start()'が呼び出されます。並行物を出力して印刷する場合、異なる順序で異なる順序で実行することがあります。 –

答えて

2

新しいスレッドができるだけ早くstart()が呼び出されると実行を開始しますので。

メインスレッドは最初のスレッドを作成して開始します。これは、他の2つのスレッドを開始するメインスレッドと、カウントダウンを開始する最初のスレッドに対して待機メッセージを出力するレースです。

誰が勝ちますか?任意で、ハードウェアに依存します。

関連する問題