美文网首页
boy-learning-thread | 1.1.1 线程状态

boy-learning-thread | 1.1.1 线程状态

作者: BruceOuyang | 来源:发表于2019-10-18 23:35 被阅读0次

    相关源码:boy-learning-thread
    个人博客:http://bruce.bugmakers.club
    内容来自《网易微专业 - 高性能编程章节》

    线程状态

    1.1.1-thread-states.png
    • New:尚未启动的线程的线程状态
    public class Demo{
        public static void main(String [] args) {
            Thread thread = new Thread(() -> {
                log.info("hello world");
            });
            log.info("thread's NEW, let's check it: {}", thread.getState().toString());     
        }
    }
    
    • Runnable:可运行线程的线程状态,等待CPU调度
    public class Demo{
        public static void main(String [] args) {
            Thread thread = new Thread(() -> {
                log.info("hello world");
            });
            thread.start();
            log.info("thread's RUNNABLE, let's check it: {}", thread.getState().toString());     
        }
    }
    
    • Blocked:线程阻塞等待监视器锁定的线程状态,处于synchronized同步代码块或方法中被阻塞
    public class Demo{
        public static void main(String [] args) {
            Thread thread = new Thread(() -> {
                synchronized (Demo.class) {
                    log.info("hello world");
                }
            });
            synchronized (Demo.class) {
                thread.start();
                log.info("thread's BLOCKED, let's check it: {}", thread.getState().toString());     
            }
        }
    }
    
    • Waiting:等待线程的线程状态。下列不带超时的方式:Object.wait、Thread.join、LockSupport.park
    public class Demo{
        public static void main(String [] args) {
            Thread thread = new Thread(() -> {
                synchronized (Demo.class) {
                    Demo.class.wait();
                    log.infoln("hello world");
                }
            });
            thread.start();
            Thread.sleep(200L);
            synchronized (Demo.class) {
                log.info("thread's WAITING, let's check it: {}", thread.getState().toString());
                Demo.class.notify();
           }
            
        }
    }
    
    • TimedWaiting:具有指定等待时间的等待线程的线程状态。下列带超时的方式:Thread.sleep、Object.wait、Thread.join、LockSupport.parkNanos、LockSupport.parkUntil
    public class Demo{
        public static void main(String [] args) {
            Thread thread = new Thread(() -> {
                try{
                    // 等待10秒
                    Thread.sleep(10000L);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                log.infoln("hello world");
            });
            thread.start();
            Thread.sleep(2000L);
            log.info("thread's TIMED_WAITING, let's check it: {}", thread.getState().toString());
        }
    }
    
    • Terminated:终止线程的线程状态。线程正常完成执行或者出现异常。
    public class Demo{
        public static void main(String [] args) {
            Thread thread = new Thread(() -> {
                log.info("hello world");
            });
            thread.start();
            Thread.sleep(1000L);
            log.info("thread's TERMINATED, let's check it: {}", thread.getState().toString());     
        }
    }
    

    相关文章

      网友评论

          本文标题:boy-learning-thread | 1.1.1 线程状态

          本文链接:https://www.haomeiwen.com/subject/nrxgmctx.html