-5

プログラムは、スレッド t1 を生成するスレッド t0 を作成し、続いてスレッド t2 および t3 が作成されt3ます。

スレッドt0t1、およびがt2中断されているのはなぜですか?

     public class Cult extends Thread 
            {  
                private String[] names = {"t1", "t2", "t3"};
                static int count = 0;
                public void run() 
                {
                    for(int i = 0; i < 100; i++) 
                    {
                        if(i == 5 && count < 3) 
                        {
                            Thread t = new Cult(names[count++]);
                            t.start();
                            try{
                                Thread.currentThread().join();
                            }
                            catch(InterruptedException e)
                            {
                                e.printStackTrace();
                            }
                        }
                        System.out.print(Thread.currentThread().getName() + " ");
                    } 
                 }

                 public static void main(String[] a`)
                 {
                     new Cult("t0").start();
                 }
            }
4

2 に答える 2

2

あなたが見逃した最も重要なポイント:

Thread.currentThread().join();

joinソース コードのメソッドはメソッドを使用しisAliveます。

public final synchronized void join(long millis) 
    ...
    if (millis == 0) {
        while (isAlive()) {
        wait(0);
        }
    ...
    }

死んだThread.currentThread().join()ときだけ戻ってくるということです。Thread.currentThread()

しかし、あなたの場合、実行中のコードThread.currentThread()自体がこのコードの平和を持っているため、それは不可能ですThread.currentThread().join()。そのため、スレッド 3 の完了後にプログラムがハングし、その後は何も起こりません。

ここに画像の説明を入力

于 2013-08-13T22:20:56.883 に答える