线程的状态

 返回主页面

线程状态间的变化可以参考 4.1中超链接的内容

package com.qdb.thinkv.thread.base.state;

import com.qdb.thinkv.thread.utils.SleepUtils;

public class ThreadState {
    
    public static void main(String[] args) {
        new Thread(new TimeWating(),"TImewatingThread").start();
        new Thread(new Waiting(),"WaitingThread").start();
        new Thread(new Blocked(),"BlockedThread-1").start();
        new Thread(new Blocked(),"BlockedThread-2").start();
    }
    
    static class TimeWating implements Runnable{

        public void run() {
            while(true){
                SleepUtils.second(100);
            }
            
        }
    }
    
    static class Waiting implements Runnable{

        public void run() {
            while(true){
                synchronized (Waiting.class) {
                    try {
                        Waiting.class.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            
        }
    }
    
    static class Blocked implements Runnable{

        public void run() {
            synchronized (Blocked.class) {
                while (true) {
                    SleepUtils.second(100);
                }
            }
            
        }
        
    }
}

 

 

package com.qdb.thinkv.thread.utils;

import java.util.concurrent.TimeUnit;

public class SleepUtils {
    public static final void second(long seconds){
        try{
            TimeUnit.SECONDS.sleep(seconds);
        }catch(InterruptedException e){
            
        }
    }
}

 

 

 

posted @ 2018-08-13 21:26  yunchong1019  阅读(93)  评论(0编辑  收藏  举报