十四.线程状态

线程的状态

  • 1.创建状态,线程一旦创建就进入到了创建状态。
Thread s1 = new Thread();
  • 2.就绪状态,当调用此方法时,线程立即进入就绪状态,但并不意味着立即调度执行
s1.start();
  • 3.阻塞状态,当调用sleep,wait方法或同步锁定时,线程就进入阻塞状态,即代码不往下执行,阻塞事件解除后,重新进入就绪状态,等待cpu调度执行。
s1.sleep();
  • 4.运行状态,进入运行状态,线程才真正执行线程体的代码块
  • 5.死亡状态,线程中断或者结束,一旦进入死亡状态,就不能被再次启动

线程的方法

setPriority(int newPriority)  更改线程优先级 
static void sleep(long millis)  在指定时间使当前线程休眠
void join()  等待该线程终止
static void yield()  暂停当前线程对象,并执行其他线程
void interrupt()  中断线程
boolean isAlive()  测试线程是否处于活动状态

1.线程停止

  • 1.建议线程正常停止,利用次数不建议死循环
  • 2.使用标志位
  • 3.不要使用stop或destroy方法
  • 代码实现
public class stopTest implements Runnable {
    //1.设置一个标志位
    private boolean flag = true;
    @Override
    public void run() {
        int i = 0;
        while (flag) {
            System.out.println("run......Thread"+i++);
        }
    }
    //2.设置公开的方法停止线程,转换标志位
    public void stop() {
        this.flag = false;
    }
    public static void main(String[] args) {
        stopTest stopTest = new stopTest();
        new Thread(stopTest).start();
        for (int i = 0; i < 1000; i++) {
            System.out.println("main"+i);
            if (i == 900) {
                //调用stop方法切换标志位,使线程停止
                stopTest.stop();
                System.out.println("线程该停止了");
            }
        }
    }
}

2.线程休眠(sleep)

  • 指定当前线程阻塞的毫秒数
  • 存在异常(InterruptedException)
  • 当达到时间后线程进入就绪状态
  • 可模拟网络延时,倒计时
  • 每个对象都有一个锁,sleep不会释放锁
  • 代码模拟延时效果
//模拟倒计时
public class sleep02Test {
    public static void main(String[] args) throws InterruptedException {
        countDown();
    }
    public static void countDown() throws InterruptedException {
        int num = 10;
        while(true) {
            Thread.sleep(1000);
            System.out.println(num--);
            if (num <= 0) {
                break;
            }
        }
    }
}

3.线程礼让(yield)

  • 礼让线程,让当前正在执行的线程暂停,但不阻塞
  • 让线程从运行状态转为就绪状态
  • 让cpu重新进行调度分配,礼让不一定成功
  • 代码实现
//测试礼让线程,礼让不一定成功
public class yieldTest {
    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()+"start");
        Thread.yield();
        System.out.println(Thread.currentThread().getName()+"stop");
    }
}

4.线程插队(vip)

  • 待此线程执行完之后,再执行其他线程
  • 举个栗子
public class joinTest implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 300; i++) {
            System.out.println("VIP is coming !"+i);
        }
    }
    public static void main(String[] args) throws InterruptedException {
        joinTest test = new joinTest();
        Thread thread = new Thread(test);//代理模式
        thread.start();
        //主线程
        for (int i = 0; i < 1000; i++) {
            if (i == 200) {
                thread.join();
            }
            System.out.println("main"+i);
        }
    }
}

线程状态观测

  • 代码实例
public class stateTest {
    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);//NEW
        //观察启动后
        thread.start();
        state = thread.getState();//变量
        System.out.println(state);//RUN
        //只要线程不会终止,就执行while语句
        while (state != Thread.State.TERMINATED) {
            Thread.sleep(1000);
            state = thread.getState();//更新线程状态
            System.out.println(state);//输出状态
        }
        thread.start();//报错,死亡的线程不能被启动
    }
}

线程的优先级(priority)

  • Java提供一个线程调度器来监控程序中启动后进入就绪状态的所有线程
  • 线程调度器按照优先级决定应该调度哪个线程来执行
  • 线程的优先级用数字来表示(1~10)
  • 获取优先级:setPriority(),getPriority(int x);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值