阅读背景:

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

来源:互联网 

/*
练习:创立两个线程,和主线程交替运行

线程都有自己默许的名称,格局为Thread-编号,编号从0开端。

static Thread currentThread():获得当前线程对象
getName():获得线程名称

设置线程名称:setName()或结构函数
*/

class Test extends Thread{
	private String name;
	Test(String name){
		//this.name = name;
		super(name);
	}
	public void run(){
		System.out.println(Thread.currentThread().getName()+" run "+x);//Thread.currentThread()==this
	}
}

class ThreadTest{
	public static void main(String[] args){
		Test t1 = new Test("one");
		Test t2 = new Test("two");
		
		t1.start();
		t2.start();
		
		for(int x = 0;x<60;x++){
			System.out.println("main "+x);
		}
	}
}/*
练习:创立两个线程,和主线程交替运行

线程都有自己默许的名称,格局为Thread-编号,编号




你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: