//测试类
package test20220512;
public class myThreadJiekou implements Runnable {
private int bao=10;
public void run() {
for(int i=0;i<10;i++) {
if(bao>0)
System.out.println(Thread.currentThread().getName()+"捡了第"+bao--+"宝贝");
}
try{
Thread.sleep(1000);
}catch (Exception e) {
System.out.println();
}
}
}
//调用类
package test20220512;
import java.io.InterruptedIOException;
public class ceshijiekou {
public static void main(String[] args) {
myThreadJiekou p=new myThreadJiekou();
Thread t=new Thread(p,"张");
t.setPriority(Thread.NORM_PRIORITY);//设置优先级
Thread t1=new Thread(p,"黎");
t1.setPriority(Thread.NORM_PRIORITY+1);
Thread t2=new Thread(p,"刘");
t2.setPriority(Thread.NORM_PRIORITY+2);
Thread t3=new Thread(p,"郭");
t3.setPriority(Thread.NORM_PRIORITY-1);
t.start();
t1.start();
t2.start();
t3.start();
System.out.println(Thread.currentThread().getName()+"执行");
try { //加入main县城,等子线程执行完,在执行主线程
t.join();
t1.join();
t2.join();
t3.join();
}catch(InterruptedException e) {
e.printStackTrace();
}finally {
System.out.println("结束");
}
}
}