首页 > 编程语言 > 详细

多线程(获取线程对象以及名称)

时间:2015-05-09 01:11:24      阅读:296      评论:0      收藏:0      [点我收藏+]

/*

创建两个线程,和主线程交替运行

原线程都有自己默认的名称

Tread-编号,该编号从0开始

static Thread currentThread()获取当前线程对象

getName():获取线程名称

设置线程名称:setName或者构造函数

*/

class Test extends Thread

{

      //private String name;

      Test(String name)

             {   

                  //this.name=name;  

                    super(name);  

             }  

        public void run()

              {  

                     for(int x=0;x<60;x++)  

                        {    

                                  System.out.println((Thread.currentThread()==this)+"..."+this.getName()+"run..."+x);  

                         }

                }

  }

public class ThreadTest {

          public static void main(String[] args)

                   {   

                        Test t1=new Test("one---");   

                        Test t2=new Test("two+++");  

                           //t1.run();  

                           //t2.run();  

                       for(int x=0;x<60;x++)  

                           {   

                                   System.out.println("main..."+x);  

                            }

              }

}

多线程(获取线程对象以及名称)

原文:http://www.cnblogs.com/binglin/p/4489174.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!