Java获取线程的对象和名称

/*获取线程对象以及名称(很有意义的)

原来线程都有自己默认的名称
Thread-编号  该编号从0开始

Thread 父类的方法
static  Thread currentThread() :获取当前线程对象  相当于this  
getName 获取线程名称
*/



class Text extends Thread
{
    //private String name;
    Text(String name)
    {
        //this.name=name;
        super(name);                   /*父类有方法定义线程的名称*/
    }
    public void run()
    {
        for(int i=0;i<60;i++)
        {
            System.out.println(name+"run---"+i);
            System.one.println(Thread.currentThread().getName()+"run---"+i);
        }
    }
}

class ThreadText
{
    public static void main(String args[])
    {
        Text t1=new Text("one");
        Text t2=new Text("two");
        t1.start();
        t2.start();
        for(int i=0;i<60;i++)
        {
            System.out.println("main run---"+i);
        }    
    }
}

posted @ 2016-07-10 17:17  Qi_Yuan  阅读(54128)  评论(0编辑  收藏  举报