多线程:线程状态

1.线程状态

线程的五大状态:

  • 创建状态

  • 就绪状态

  • 阻塞状态

  • 运行状态

  • 死亡状态

线程状态6种(官方)

        为什么线程状态既有5中又有6种呢,官方推荐的状态是6中,5种状态只不过是针对OS操作系统的线程状态,Java线程将操作系统线程状态的RUNNABLE,RUNNING, BLOCKED合并为一个状态:Runnable状态,

6种状态分别为:

new、runnable、blocked、waiting、timed waiting、terminated

public class Teststate {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{
            for(int i = 0;i < 5;i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("********************");
            }
        });    
        Teststate t2 = new Teststate();
        
        State state = t.getState();
        System.out.println(state);
        
        t.start();
        state = t.getState();
        System.out.println(state);
        
        while(state != t.getState().TERMINATED) {
            Thread.sleep(100);
            state = t.getState();
            System.out.println(state);
        }
    }
}

2.线程方法

方法说明
setPriority(int newPriority)更改线程的优先级
static void sleep(long milis)设定休眠时间
void join()相当于插队
static void yield()礼让
void interrupt()中断线程(不推荐)
boolean isAlive()查看线程是否存活

2.1终止线程

对于停止线程,不推荐使用jdk提供的stop(),建议设一个flag标志来终止线程运行。如下demo所示:

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 t = new Teststop();
        new Thread(t).start();    
        for(int i = 0 ; i <= 10; i++) {
            System.out.println("main"+i);
            if(i == 1) {
                t.stop();
                System.out.println("--------------------------------------------线程停止了");
            }
        }
    }
}

2.2sleep()

public class Testsleep implements Runnable{
    @Override
    public void run() {}
    //倒计时10个数
    public static void tenDown() throws InterruptedException {
        int ten = 10;    
        while(true) {
            if(ten <= 0) {
                break;
            }
            Thread.sleep(1000);
            System.out.println(ten--);
        }    
    }
    public static void main(String[] args) throws InterruptedException{
        tenDown();
//        Testsleep t = new Testsleep();
//        new Thread(t,"x1").start();
//        new Thread(t,"x2").start();
//        new Thread(t,"x3").start(); 
        //显示当前系统时间
        Date date = new Date(System.currentTimeMillis());
        while(true) {
            Thread.sleep(1000);
            String sdf = new SimpleDateFormat("HH:mm:ss").format(date);
            System.out.println(sdf);
            date = new Date(System.currentTimeMillis());
        }    
    }
}

2.3yield()

public class TestYield {
    public static void main(String[] args) {
            MyYield y = new MyYield();    
            new Thread(y,"a").start();
            new Thread(y,"b").start();
    }
}
class MyYield implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"线程开始");
        Thread.yield();
        System.out.println(Thread.currentThread().getName()+"线程开始");
    }
}

2.4join()

相当于插队

public class Testjoin implements Runnable{
    @Override
    public void run() {
        for(int i = 0;i < 5;i++) {
            System.out.println("vip线程"+i);
        }
    }
    public static void main(String[] args) throws InterruptedException {
        Testjoin tj = new Testjoin();        
        Thread t = new Thread(tj);        
        t.start();
        for(int i = 0;i < 10;i++) {
            if(i==5) {
                t.join();
            }
            System.out.println("普通线程"+i);
        }
    }
}

2.5线程守护:Daemon

public class TestDaemon {
    public static void main(String[] args) {
        Human h = new Human();
        God g = new God();        
        Thread t1 = new Thread(g);
        t1.setDaemon(true);
        t1.start();
        new Thread(h).start();    
    }
}
class God implements Runnable{
    @Override
    public void run() {
        while(true) {
            System.out.println("保佑中!!");
        }
    }    
}
class Human implements Runnable{
    @Override
    public void run() {
        for(int i = 0 ; i < 36500; i++) {
            System.out.println("human---");
        }
        System.out.println("活着");
    }    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值