JavaSE(2)03 多线程--线程状态及方法

多线程

线程的五大状态

在这里插入图片描述

  • 创建状态:new
  • 就绪状态:(start()开启线程进入就绪状态)
  • 运行状态:RUNNABLE
  • 阻塞状态:TIMED_WAITING
  • 死亡状态:TERMINATED 线程一旦进入该状态就不能再调start()启动
    在这里插入图片描述

线程方法

线程停止
  • 建议线程正常停止—>利用次数,不建议死循环
  • 建议使用标志位—>设置一个标志位
  • 不建议使用stop或destroy等过时的或JDK不建议使用的方法
    //1、设置标志位
    private Boolean flag=true;
    @Override
    public void run() {
        int i=0;
        while (flag){
            System.out.println(Thread.currentThread().getName()+"is Running"+i);
            i++;
        }
    }
//2、设置公开的方法停止线程
    public  void stop(){
        this.flag=false;
    }
    public static void main(String[] args) {
        ThreadStop threadStop = new ThreadStop();
        new Thread(threadStop).start();
        for (int i = 0; i < 1000; i++) {
            System.out.println("mainThread is Running"+i);
            if(i==900){
                threadStop.stop();
                System.out.println("is stopped");
            }
        }
    }
线程休眠
  • sleep(时间):指定当前线程阻塞的毫秒数
  • sleep存在异常InterruptedException
  • sleep时间到达后线程进入就绪状态
  • sleep可以模拟网络延时,倒计时等
  • 每一个对象都有一把锁,sleep不会释放锁
int i=10;
        while (i>0){
            System.out.println(new SimpleDateFormat("yy-mmmm-dd hh:mm:ss").format(date));
            Thread.sleep(1000);
            date=new Date(System.currentTimeMillis());
            i--;
        }
线程礼让
  • 礼让线程,让当前真正执行的线程暂停,但不阻塞
  • 让线程从运行态转为就绪状态
  • 让CPU重新调度,礼让不一定成功,看CPU心情
//礼让不一定成功
public class ThreadYield {
    public static void main(String[] args) {
        YieldTest yieldTest=new YieldTest();
        new Thread(yieldTest,"zs").start();
        new Thread(yieldTest,"ls").start();

    }
}
class YieldTest implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"线程开始执行");
        Thread.yield();//礼让
        System.out.println(Thread.currentThread().getName()+"线程停止执行");
    }
}

在这里插入图片描述

线程强制执行
  • Join合并线程,待此线程执行完毕后,在执行其他线程,其他线程阻塞
  • 可以想象成插队(让之前执行的线程进入阻塞状态)
public class ThreadJoin {
    public static void main(String[] args) throws InterruptedException {
        TestJoin testJoin=new TestJoin();
        Thread thread=new Thread(testJoin);
       thread.start();
        for (int i = 0; i < 50; i++) {
            if(i==30) {
                thread.join();//main线程阻塞,等thread线程执行结束后main继续用执行
            }
            System.out.println("main。。。。。。"+i);
        }
    }
}
class TestJoin implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 200; i++) {
            System.out.println("join...."+i);
        }
    }
}
线程状态
    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("*********");
        });
        //观察状态
        System.out.println(thread.getState());//NEW
        thread.start();
        System.out.println(thread.getState());//RUNNABLE
        while (thread.getState()!=Thread.State.TERMINATED){//只要线程不终止,就一直输出状态
            Thread.sleep(1000);
            System.out.println(thread.getState()) ;
        }
    }
线程的优先级
  • Java提供了一个线程调度器来监控程序中启动后进入就绪态的所有线程,线程调度器按照优先级决定应该调度哪个线程来执行。
  • 线程的优先级用数字表示,范围从1~10
  • 获取/改变线程优先级 .setPriority() .getPriority()
  • 优先级低只意味着获得调度的概率低,并不是优先级低就不会被调度,这都是看CPU的调度
守护线程(daemon)
  • 线程分为用户线程和守护线程
  • 虚拟机必须确保用户线程执行完毕
  • 虚拟机不用等待守护线程执行完毕
public class ThreadDaemon {
    public static void main(String[] args) {
        Thread thread=new Thread(new You1());
        Thread thread1=new Thread(new God());
        thread1.setDaemon(true);//默认为false表示用户线程,正常线程都是用户线程
        thread1.start();
        thread.start();
    }
}
class You1 implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("开开心心每一年");
        }
        System.out.println("goodBye,world");
    }
}
class God implements Runnable{
    @Override
    public void run() {
        while(true){
            System.out.println("上帝保佑");
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值