李さんは共有します:javaスレッドのライフサイクル2


≪インスタンス|Instance|emdw≫


次に例を示します.
package demo.ch;

public class UserThread extends Thread {
    UserThread() {
        super();
    }

    UserThread(String name) {
        super(name);
    }

    public void run() {
        System.out.println("thread started running..");
    }

    public static void main(String[] args) {
        UserThread thread1 = new UserThread("Thread1");
        UserThread thread2 = new UserThread("Thread2");

        System.out.println("Thread 1 initial name and priority");
        System.out.println("name:" + thread1.getName());
        System.out.println("priority:" + thread1.getPriority());

        System.out.println("Thread 2 initial name and priority");
        System.out.println("name:" + thread2.getName());
        System.out.println("priority:" + thread2.getPriority());
        System.out.println("");

        thread1.setPriority(6);
        thread2.setPriority(9);

        System.out.println("Thread 1 initial name and priority");
        System.out.println("name:" + thread1.getName());
        System.out.println("priority:" + thread1.getPriority());

        System.out.println("Thread 2 initial name and priority");
        System.out.println("name:" + thread2.getName());
        System.out.println("priority:" + thread2.getPriority());
        System.out.println("");

        thread1.start();
        thread2.start();

        for(int i=0; i<5; i++)
            System.out.println("main method i value: " + i);
    }
}

出力結果:
Thread 1 initial name and priority
name:Thread1
priority:5
Thread 2 initial name and priority
name:Thread2
priority:5

Thread 1 initial name and priority
name:Thread1
priority:6
Thread 2 initial name and priority
name:Thread2
priority:9

main method i value: 0
main method i value: 1
thread started running..
main method i value: 2
thread started running..
main method i value: 3
main method i value: 4