March 29th, 2015, Thread Name is odd by increasing 1
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); } }