查看线程的状态

1. 查看NEW、RUNNABLE、TERMINATED

package thread.state;

/**
 * @author cxyxh
 * @date 2021-07-27
 */
public class NewRunnableTerminatedState {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            System.out.println("测试runnable: " + Thread.currentThread().getState());
        });
        System.out.println("测试new:" + thread.getState());
        thread.start();
        Thread.sleep(1000);
        System.out.println("测试terminated:" + thread.getState());
    }
}

2. 查看BLOCKED状态

package thread.state;

/**
 * @author cxyxh
 * @date 2021-07-27
 */
public class BlockedState {

    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(() -> {
            try {
                Thread.sleep(100);
                synchronized(BlockedState.class){
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        
        Thread thread2 = new Thread(() -> {
            try {
                //让thread2后进入同步代码块
                Thread.sleep(200);
                synchronized(BlockedState.class){
                    Thread.sleep(300);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        thread1.start();
        thread2.start();

        Thread.sleep(300);
        System.out.println("测试blocked:" + thread2.getState());
    }
}

3. 查看WAITING状态

package thread.state;

import java.util.concurrent.locks.LockSupport;

/**
 * @author cxyxh
 * @date 2021-07-27
 */
public class WaitingState {

    public static void main(String[] args) throws InterruptedException {
        //LockSupport.park()
        //保证打印的时候处于thread1处于park状态
        Thread thread1 = new Thread(LockSupport::park);
        Thread thread2 = new Thread(() -> {
            try {
                Thread.sleep(300);
                LockSupport.unpark(thread1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        thread1.start();
        thread2.start();
        Thread.sleep(100);
        System.out.println("处于park状态:" + thread1.getState());


        //join(),无参数
        //保证thread4处于join中
        Thread thread3 = new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread thread4 = new Thread(() -> {
            try {
                thread3.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        thread3.start();
        thread4.start();

        Thread.sleep(500);
        System.out.println("等待join完成:" + thread4.getState());

        //wait(),无参数
        //保证thread6处于wait中
        Object lock = new Object();

        Thread thread5 = new Thread(() -> {
            synchronized (lock){
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread thread6 = new Thread(() -> {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (lock){
                lock.notify();
            }
        });

        thread5.start();
        thread6.start();
        Thread.sleep(300);

        System.out.println("处于wait,等待被唤醒:" + thread5.getState());
    }
}

4. 查看TIMED_WAITING状态

package thread.state;

/**
 * @author yangww
 * @date 2021-07-27
 */
public class TimeWaitingState {
    public static void main(String[] args) throws InterruptedException {
        //sleep
        Thread thread1 = new Thread(() -> {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        thread1.start();
        Thread.sleep(100);

        System.out.println("处于sleep状态:" + thread1.getState());



        //wait(100),有参数的
        Object lock = new Object();
        Thread thread2 = new Thread(() -> {
            synchronized (lock){
                try {
                    lock.wait(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread thread3 = new Thread(() -> {
            try {
                Thread.sleep(200);
                synchronized (lock){
                    lock.notify();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        thread2.start();
        thread3.start();
        Thread.sleep(100);
        System.out.println("处于wait有参数的状态:" + thread2.getState());

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值