线程状态
五大状态
![]()
![]()
线程方法
| 方法 |
说明 |
| setPriority(int newPriority) |
更改线程的优先级 |
| static void sleep(long millis) |
在指定的毫秒数内让当前正在执行的线程休眠 |
| void join() |
等待该线程终止()插队 |
| static void yield() |
暂停当前正在执行的线程对象,并执行其他线程(礼让) |
| void interrupt() |
中断线程 |
| boolean isAlive() |
测试线程是否出于活动状态 |
停止线程
- 不推荐使用JDK提供的stop()、destroy()方法
- 推荐线程自己停止
- 建议使用一个标志位进行终止变量,当flag=false,则终止线程运行
public class TestStop implements Runnable{
//设置一个标志位
private boolean flag = true;
@Override
public void run() {
int i = 0;
while (flag){
System.out.println("run thread"+i++);
}
}
public void stop(){
this.flag=false;
}
public static void main(String[] args) {
TestStop testStop = new TestStop();
new Thread(testStop).start();
for (int i = 0; i < 1000; i++) {
System.out.println("main"+i);
if (i==900){
testStop.stop();
System.out.println("线程停止");
}
}
}
}
线程休眠
- sleep指当前线程阻塞的毫秒数ms
- sleep存在异常InterruptedException
- sleep时间到达后线程进入就绪状态
- sleep可以模拟网络延迟、倒计时等
- 每个对象都有一个锁,sleep不会释放锁
//模拟倒计时
public class TestSleep{
public static void timeDown() throws InterruptedException {
int num=5;
while (true){
Thread.sleep(1000);
System.out.println(num--);
if (num<=0){break;}
}
}
public static void main(String[] args) throws InterruptedException {
try {
timeDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
线程礼让
- 礼让线程,让当前线程正在执行的线程暂停,但不阻塞
- 将线程从运行状态转为就绪状态
- 让CPU重新调度,礼让不一定成功(看CPU心情)
public class TestYield {
public static void main(String[] args) {
MyYield myYield = new MyYield();
new Thread(myYield,"a").start();
new Thread(myYield,"b").start();
}
}
class MyYield implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"线程开始执行");
Thread.yield();
System.out.println(Thread.currentThread().getName()+"线程停止执行");
}
}
Join
- Join合并线程,待此线程执行完成后再执行其他线程,其他线程阻塞
- 可理解为插队
//Join
public class TestJoin {
public static void main(String[] args) throws InterruptedException {//Join在使用时IDEA提示要捕获异常
JoinVip joinVip = new JoinVip();
Thread thread = new Thread(joinVip);
thread.start();//线程开始,与主线程一起
for (int i = 0; i < 100; i++) {//主线程要执行的程序
if(i==30){
thread.join();
}
System.out.println("普通线程"+i);
}
}
}
class JoinVip implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("vip线程"+i);
}
}
}
线程状态观测
public class TestState {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()->{
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("////////"+i);
}
});
//观测状态
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);
}
}
}
线程优先级
- Java提供一个线程调度器来监控程序汇总启动后进入就绪状态的所有线程,线程调度器按优先级决定应该调度哪个线程
- 线程的优先级用数字表示,范围从1-10
- Thread.MIN_PRIORITY=1;
- Thread.MAX_PRIORITY=10;
- Thread.NORM_PRIORITY=5;
- 使用以下方式改变或获取优先级
- getPriority().setPriority(int xx)
- 注意:优先级低只是意味着获得调度的概率低,并不是不被调用,有可能会发生性能倒置的情况
package com.thread.state;
public class TestPriority extends Thread{
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
MyPriority myPriority = new MyPriority();
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);
t1.start();
t2.setPriority(1);
t2.start();
t3.setPriority(1);
t3.start();
t4.setPriority(10);
t4.start();
t5.setPriority(1);
t5.start();
}
}
class MyPriority implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
}
}
守护(daemon)线程
- 线程分为用户线程和守护线程
- 虚拟机必须确保用户线程执行完毕
- 虚拟机不用等待守护线程执行完毕
- 包括:后台记录操作日志、监控内存、垃圾回收等
thread.setDaemon(boolean);
//默认是false(即用户进程),设置为true则变成守护进程