boy-learning-thread | 1.1.1 线程状态

2019-10-18  本文已影响0人  BruceOuyang

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

线程状态

1.1.1-thread-states.png
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());     
    }
}
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());     
    }
}
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());     
        }
    }
}
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();
       }
        
    }
}
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());
    }
}
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());     
    }
}
上一篇下一篇

猜你喜欢

热点阅读