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);
}
}
}
线程练习(两个线程交替运行)
最新推荐文章于 2022-09-16 15:29:46 发布