package cn.zxx.base;
public class Thread1 implements Runnable {
@Override
public void run() {
for(int i=0;i<11;i++){
/**
* Thread.currentThread().getName()的方法指获取当前线程名称
*/
System.out.println("线程:"+Thread.currentThread().getName()+"\b次数:"+i);
/**
* 线程礼让:
yield()方法只能让同优先级的线程有执行的机会
*/
if(i == 5){
System.out.println("暂停...");
Thread.currentThread().yield();
}
/**
* Thread.sleep()的方法是让线程休眠
*/
try {
Thread.sleep(3600);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package cn.zxx.base;
public class Test1 {
public static void main(String[] args) {
Thread1 t1 = new Thread1();
Thread th1 = new Thread(t1,"t1");
//设置最低优先级,表示常量(1)
th1.setPriority(Thread.MIN_PRIORITY);
th1.start();
Thread1 t2 = new Thread1();
Thread th2 = new Thread(t2,"t2");
//设置中等优先级,表示常量(5)
th2.setPriority(Thread.NORM_PRIORITY);
th2.start();
Thread1 t3 = new Thread1();
Thread th3 = new Thread(t3,"t3");
//设置最高优先级,表示常量(10)
th2.setPriority(Thread.MAX_PRIORITY);
th3.start();
}
}
![](https://img-blog.csdnimg.cn/20190320183521172.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxNzA5NDk0,size_16,color_FFFFFF,t_70)