码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-03-30 01:08:43      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

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);
    }
}

 

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

标签:

原文地址:http://www.cnblogs.com/lionfight/p/4376738.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!