ThreadStatusDemo.java 1.4 KB
Newer Older
L
summit  
liulei33 已提交
1
package com.Mrliu.thread.base;
L
liulei33 已提交
2

L
summit  
liulei33 已提交
3
import java.util.ArrayList;
L
liulei33 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
import java.util.concurrent.TimeUnit;

/**
 * 线程状态演示,jps,jstack
 */
public class ThreadStatusDemo {

    public static void main(String[] args) {
        new Thread(()->{
           while (true){
               try {
                   TimeUnit.SECONDS.sleep(1);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
        },"Time_Waiting_Thread").start();

        new Thread(()->{
            while (true){
                synchronized (ThreadStatusDemo.class){
                    try {
                        ThreadStatusDemo.class.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        },"Waiting_Thread").start();

        new Thread(new BlockedDemo(),"Block01_Thread").start();
        new Thread(new BlockedDemo(),"Block02_Thread").start();
    }

    static class BlockedDemo implements Runnable{

        @Override
        public void run() {
            while (true){
                synchronized (BlockedDemo.class){
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

}