多线程-线程状态-中级进化04

多线程-线程状态-中级进化04

线程的5种状态

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FWyxsXWd-1600053055673)(/Users/chenxiwen/Library/Application Support/typora-user-images/image-20200907092936502.png)]

方法

setPriority:更改线程优先级

sleep

join:等待该线程终止

yield:暂停当前线程,让其他线程先行

isAlive:测试线程是否存活

线程的停止

使用标识位,让线程自己停止

不推荐使用JDK提供的stop()

建议使用一个标示位进行种植变量,当flag=false,则终止线程运行

public class TestStop implements Runnable{
    
    

    private boolean flag = true;

    @Override
    public void run() {
    
    
        int i = 0;
        while (flag) {
    
    
            System.out.println("线程"+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("stop");
            }
        }

    }
}

线程的休眠

sleep(时间)指定当前线程阻塞的毫秒数

sleep存在异常InterruptedException

sleep时间达到后线程进入就绪状态

sleep可以模拟网络延时,倒计时等

每一个对象都有1个锁,sleep不会释放锁

public static void main(String[] args) {
    try {
        tendown();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    //打印当前系统时间
    Date startTime = new Date(System.currentTimeMillis());
    while (true) {
        try {
            Thread.sleep(1000);
            System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime));
            startTime = new Date(System.currentTimeMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public static void tendown() throws InterruptedException {
    int num =10;
    while (true) {
        Thread.sleep(1000);
        System.out.println(num--);
        if (num < 0) {
            break;
        }
    }
}

线程礼让Yield

礼让线程,让当前正在执行的线程暂停,但不阻塞

将线程从运行状态转为就绪状态

让CPU重新调度,礼让不一定成功,看CPU心情

线程插队Join

Join合并线程,待次线程执行完成,再执行其他线程,其他线程阻塞

可以想象成插队

//join,插队
public class TestJoin implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("vip来了"+i);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        TestJoin testJoin = new TestJoin();
        Thread thred = new Thread(testJoin);
        thred.start();

        for (int i = 0; i < 1000; i++) {
            if (i == 20) {
                thred.join();
            }
            System.out.println("main"+i);
        }
    }
}

线程的状态观测

Thread State

线程状态:线程可以处于一下状态之一

  • NEW
    尚未启动的线程处于此状态。
  • RUNNABLE
    在Java虚拟机中执行的线程处于此状态。
  • BLOCKED
    被阻塞等待监视器锁定的线程处于此状态。
  • WAITING
    正在等待另一个线程执行特定动作的线程处于此状态。
  • TIMED_WAITING
    正在等待另一个线程执行动作达到指定等待时间的线程处于此状态。
  • TERMINATED
    已退出的线程处于此状态。
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();
                }
            }
        });

        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);
        }

    }

}

线程优先级priority

java提供一个线程调度器来监控程序中启动后进入就绪状态的所有线程,线程调度器按照优先级决定应该调度那个线程先执行

优先级用数字来表示,范围从1-10

Thread.MIN_PRIORITY=1;

Thread.MAX_PRIORITY=10;

Thread.NORM_PRIORITY=5;

改变或获取优先级

getPriority,setPriority(int i)

优先级的设定建议在start()调度钱

优先级低只意味调度概率低,并不是不会调用,还是取决CPU调度

守护线程daemon

线程分为用户线程和守护线程

虚拟机必须确保用户线程执行完毕

虚拟机不用等待守护线程执行完毕

如后台记录操作日志,监控内存,垃圾回收等待

setDaemon(true);

猜你喜欢

转载自blog.csdn.net/rr18758236029/article/details/108575618