4.1 线程状态观测&&线程优先级
4.1.1 线程状态观测
- Thread.State
线程状态。线程可以处于以下几种状态
- NEW :尚未启动的线程处于此状态 (新生状态(创建状态)/就绪状态)
- RUNNABLE :在 Java 虚拟机中 执行的 线程处于 此 状态。
- BLOCKED:被 “阻塞等待监视器” 锁定的线程 处于此状态。
- WAITING:正在等待另一个线程执行特定动作 的线程处于此状态。
- TIMED_WAITING:正在等待另一个线程执行动作达到指定等待时间的线程处于 此状态。
- TERMINATED:已退出的线程处于此状态。
语法格式:
线程对象.getState();//就可以获取 这个线程当前的状态
Thread state 是 getState() 获取到的数据 的 类型!
1.NEW 状态
package www.muquanyu.lesson03;public class StateDemo {
public static void main(String[] args) {
Thread thread = new Thread(()->{
for(int i = 0;i<10;++i){
try {
Thread.sleep(1000);} catch (InterruptedException e) {
e.printStackTrace();}System.out.println("");}});//观察状态Thread.State state = thread.getState();System.out.println(state);}
}
2.RUNNABLE WAITING TERMINATED 状态
package www.muquanyu.lesson03;public class StateDemo {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()->{
for(int i = 0;i<10;++i){
try {
Thread.sleep(1000);} catch (InterruptedException e) {
e.printStackTrace();}System.out.println("");}});//观察状态Thread.State state = thread.getState();System.out.println(state);//启动线程thread.start();state = thread.getState();System.out.println(state);//只要线程不终止while(state !=Thread.State.TERMINATED){
Thread.sleep(100);state = thread.getState();System.out.println(state);}}
}
输出结果:
NEW
RUNNABLE
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TERMINATED
4.1.2 线程优先级
- Java 提供一个线程调度器来监控程序中启动后进入就绪装填的所有线程,线程调度器按照优先级决定应该调度哪个线程来执行。
也就是说,Java 故意 模拟了一个类似于 CPU 调度器的东西。但是呢 ~ 实际上 最后 还是得 看 CPU 的调度。那为什么还要弄这样一个 线程调度器呢?
答:这是因为,即使 最后是CPU进行调度,但经过 Java 人为的先行处理,会让优先级高的 稍后进入就绪,这样CPU 在调度的时候,就会极大概率下 把 优先级高的最先调度。(记住,是大概率情况下!!!)
- 优先级设置 超过 10 和 小于 1 会报错。
t5.setPriority(0);t5.start();t6.setPriority(11);t6.start();
- 优先级 只能增大 优先的概率!
package www.muquanyu.lesson03;//测试线程的优先级
public class PriorityDemo {
public static void main(String[] args) {
//主线程默认优先级System.out.println(Thread.currentThread().getName() + "--->" + Thread.currentThread().getPriority());MyPriorityDemo MyPriority = new MyPriorityDemo();Thread t1 = new Thread(MyPriority);Thread t2 = new Thread(MyPriority);Thread t3 = new Thread(MyPriority);Thread t4 = new Thread(MyPriority);Thread t5 = new Thread(MyPriority);Thread t6 = new Thread(MyPriority);//以下是错误的 代码堆积形式//设置优先级t1.start();t2.setPriority(1);t2.start();t3.setPriority(4);t3.start();t4.setPriority(Thread.MAX_PRIORITY);t4.start();t5.setPriority(6);t5.start();t6.setPriority(8);t6.start();}
}class MyPriorityDemo implements Runnable{
@Overridepublic void run() {
System.out.println(Thread.currentThread().getName() + "--->" + Thread.currentThread().getPriority());}
}
问题:由于 它们是在 主线程里启动子线程的,所以 运行代码的顺次步骤,也可能会对CPU调度的顺序进行 影响。(也就是说即使在没有进行优先级的设置情况下, 谁的 .start()代码 写在前面,谁就可能 先被调度。而且这种问题会影响到 设置优先级的概率!!!)
为了避免这种情况,我们一般 要保证 所有线程 的启动代码,都要根据设置的优先级顺次的堆积在一起来写!
package www.muquanyu.lesson03;//测试线程的优先级
public class PriorityDemo {
public static void main(String[] args) {
//主线程默认优先级System.out.println(Thread.currentThread().getName() + "--->" + Thread.currentThread().getPriority());MyPriorityDemo MyPriority = new MyPriorityDemo();Thread t1 = new Thread(MyPriority);Thread t2 = new Thread(MyPriority);Thread t3 = new Thread(MyPriority);Thread t4 = new Thread(MyPriority);Thread t5 = new Thread(MyPriority);Thread t6 = new Thread(MyPriority);//设置优先级(正确的设置优先级方式)t2.setPriority(1);t3.setPriority(4);t4.setPriority(Thread.MAX_PRIORITY);t5.setPriority(6);t6.setPriority(8);
//正确的 线程方式(如果你设置了优先级,那么启动的顺序就按照 优先级的顺序来写!)t4.start();t6.start();t5.start();t1.start();t3.start();t2.start();}
}class MyPriorityDemo implements Runnable{
@Overridepublic void run() {
System.out.println(Thread.currentThread().getName() + "--->" + Thread.currentThread().getPriority());}
}
上述这种方式,是在 设置优先级之后,最正确的 写法!!但是也保证不了 CPU 调度的情况一定按照优先级来走。
看到这张图了吗?线程 1 明显 比 明显 2 优先级低,可是 CPU 先调度的 线程 1!!