March 29th, 2015, Thread Name is odd by increasing 1

2683 ワード

public class Fabric extends Thread{

    

    public static void main(String args[]){

        Thread t = new Thread(new Fabric());

        Thread t2 = new Thread(new Fabric());

        Thread t3 = new Thread(new Fabric());

        

        t.start();

        t2.start();

        t3.start();

    }



    public void run(){

        System.out.println(Thread.currentThread().getName() + ": " + i);

    }

}

このコードの出力は1,3,5(無秩序)です.
ただし、スレッド名を指定すると1,2,3になります.
public class Fabric extends Thread{

    

    public static void main(String args[]){

        Thread t = new Thread(null, new Fabric(), "1");

        Thread t2 = new Thread(null, new Fabric(), "2");

        Thread t3 = new Thread(null, new Fabric(), "3");

        

        t.start();

        t2.start();

        t3.start();

    }



    public void run(){

        System.out.println(Thread.currentThread().getName() + ": " + i);

    }

}