package com.hfxt.p3;
public class MyThread extends Thread {
public void run(){
for(int i=1;i<=5;i++){
//this.getName显示线程名
System.out.println(this.getName()+":"+i);
}
}
}
--------------------------------------------
package com.hfxt.p3;
//线程测试
public class ThreadTest {
public static void main(String[] args) {
for(int i=1;i<=10;i++){
if(i==5){
MyThread mt = new MyThread();
mt.start();
try {
//阻塞线程(当i=5时候,阻塞线程)
mt.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//Thread.currentThread()返回线程对象
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
指定线程加入当前线程
最新推荐文章于 2021-02-16 06:01:07 发布