JAVA线程的多种状态

 线程的状态图

new状态(新建状态):

创建了一个线程的对象,但是这个线程没有启动start,那么此时这个线程的状态就是NEW也就是新建状态

此时线程对象就是一个普通的JAVA对象,CPU还没有给其分配资源

public class Main16 {
    public static void main(String[] args) {
        Thread t=new Thread(()->{
            System.out.println("hello thread");
        });
        System.out.println(t.getState());
    }
}

Terminated(终止状态):

线程处于终止状态表示它已经执行完任务或者被提前中断。当线程的 run() 方法执行完毕或者调用了 Thread.interrupt() 方法中断线程时,线程会进入终止状态。

线程执行完毕的终止状态:

public class Main16 {
    public static void main(String[] args) throws InterruptedException {
        Thread t=new Thread(()->{
            System.out.println("hello thread");
        });
        System.out.println(t.getState());
        t.start();
        Thread.sleep(3000);
        System.out.println(t.getState());
    }
}

通过Thread.interrupt()终止线程

public class Main17 {
    public static void main(String[] args) throws InterruptedException {
        Thread t=new Thread(()->{
           while (Thread.currentThread().isInterrupted()){
               System.out.println("hello thread");
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
           }
        });
        System.out.println("start之前的状态:"+t.getState());
        t.start();
   Thread.sleep(3000);
        System.out.println("main线程尝试终止t线程");
        t.interrupt();
        System.out.println("终止t线程的状态"+t.getState());
    }
}

Runnable状态(就绪状态):

 RUNNABLE状态表示就绪状态,意思是该状态的线程随时都可以被调度到CPU上运行或者已经在CPU上运行。

当线程正在CPU上运行时:

public class Main18 {
    public static void main(String[] args) {
        Thread t=new Thread(()->{
           while(true){
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
           }
        });
        t.start();
        System.out.println(t.getState());
    }
}

 当线程随时可以去CPU上运行时(还没去):

public class Main18 {
    public static void main(String[] args) throws InterruptedException {
        Thread main=Thread.currentThread();
        Thread t=new Thread(()->{
            try {
                main.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            while(true){
               System.out.println("hello thread");
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
           }
        });
        t.start();
        System.out.println(t.getState());
    }
}

 让t线程等待main线程执行完毕再去执行,t线程等待的过程状态就是RUNNABLE状态

TIMED_WAITING(超时等待):

这种状态属于带时间的线程阻塞,当阻塞时间一过就可以恢复至RUNNABLE状态

public class Main19 {
    public static void main(String[] args) throws InterruptedException {
Thread t=new Thread(()->{
   while(true){

   }
});
        t.start();
        Thread.sleep(1000000);
    }
}

当main线程处于带时间的阻塞状态时,状态为TIMED_WAITING

带时间的阻塞可以是sleep也可以是join,必须加上参数才是带时间的阻塞

WAITING(等待状态):

这种状态一般是不带时间的等待

public class Main20 {
    public static void main(String[] args) throws InterruptedException {
        Thread t=new Thread(()->{
           while(true){
           }
        });
        t.start();
        t.join();
    }
}

只要t线程不结束,那么main就一直处于WAITING状态

BLOCKED(阻塞状态):

线程进入阻塞状态通常是因为某些原因导致了线程无法继续执行。常见的阻塞原因包括等待I/O操作完成、等待获取锁、等待条件满足等。当阻塞的原因消失后,线程会重新进入就绪状态等待执行。

下面是一个死锁的代码:

public class Main4 {
    public static void main(String[] args) {
        Object locker1=new Object();
        Object locker2=new Object();

        Thread t1=new Thread(()->{
           synchronized (locker1){
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
               synchronized (locker2){
                   System.out.println("t1两线程都获取到");
               }
           }
        });

        Thread t2=new Thread(()->{
            synchronized (locker2){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (locker1){
                    System.out.println("t2两线程都获取到");
                }
            }
        });
        t1.start();
        t2.start();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值