ThreadMethodTest类
public class ThreadMethodTest {
public static void main(String[] args) {
HelloThread h1 = new HelloThread("Thread:1");//利用构造器给线程起名
// h1.setName("线程一");//设置线程的名字的方式一
h1.start();
//给主线程命名
Thread.currentThread().setName("主线程");
for(int i=0;i<100;i++){
if(i%2==0){
System.out.println(Thread.currentThread().getName()+":"+i);
}
if(i==20){
try {
h1.join();//主线程阻塞,20后全是Thread1线程,直到Thread1结束
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println(h1.isAlive());
}
}
class HelloThread extends Thread{
@Override
public void run() {
for(int i=0;i<100;i++){
if(i%2==0){
try {
sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
public HelloThread(String name) {//给线程起名的方式二:利用构造器
super(name);
}
}



