7

私は現在、Java でスレッドの基本を学んでおり、簡単なスレッド グループ プログラムを作成しようとしています。異なるタイプの出力が得られますが、チュートリアルのウェブサイトと同じように書きました。以下は、異なる出力を得ている私のコードです。

public class ThreadGroupDemo implements Runnable {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
        // get the name of the current thread.
    }

    public static void main(String[] args) {
        ThreadGroupDemo runnable = new ThreadGroupDemo();
        ThreadGroup tg1 = new ThreadGroup("Parent Group");
        // Creating thread Group.

        Thread t1 = new Thread(tg1, new ThreadGroupDemo(), "one");
        t1.start();
        t1.setPriority(Thread.MAX_PRIORITY);

        Thread t2 = new Thread(tg1, new ThreadGroupDemo(), "second");
        t2.start();
        t2.setPriority(Thread.NORM_PRIORITY);

        Thread t3 = new Thread(tg1, new ThreadGroupDemo(), "Three");
        t3.start();

        System.out.println("Thread Group name : " + tg1.getName());
        tg1.list();
    }

}

出力を取得しています

Thread Group name : Parent Group
Three
java.lang.ThreadGroup[name=Parent Group,maxpri=10]
    second
one
Thread[one,10,Parent Group]
    Thread[second,5,Parent Group]
    Thread[Three,5,Parent Group]

出力は次のようになります。

one
two
three
Thread Group Name: Parent ThreadGroup
java.lang.ThreadGroup[name=Parent ThreadGroup,maxpri=10]
    Thread[one,5,Parent ThreadGroup]
    Thread[two,5,Parent ThreadGroup]
    Thread[three,5,Parent ThreadGroup] 

なぜこれが起こっているのか理解できませんか?優先順位を設定すると、それを助けることができますか?

4

1 に答える 1