您的位置:首页 > 其它

线程练习(两个线程交替运行)

2015-04-20 21:59 162 查看
package process;

/*
* 创建两个线程,和主线程交替运行
*
* currentThread():获取当前线程对象;
* getName();获取线程名称;
*
* 设置线程名称:setName();
*/

class Test extends Thread{

//private String name;
Test(String name){
//this.name = name;
super(name);//查看api
}
public void run(){
for(int x = 0;x < 100;x++){
//System.out.println(name + "test is " + x);
//System.out.println(this.getName() + "test is " + x);//每个线程有自己默认的名称this.getName();
System.out.println(Thread.currentThread().getName() + "test is " + x);//currentThread()线程对象 ==this.getName();
}
}
}
class ThreadDemo2 {

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 < 100; x++){
System.out.println("main is " + x);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: