zoukankan      html  css  js  c++  java
  • 线程的状态

     返回主页面

    线程状态间的变化可以参考 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){
                
            }
        }
    }

  • 相关阅读:
    Java学习之路(四)
    HTML学习之canves元素
    一些常用的SQL查询语句
    数据库实现动态表头
    Java学习之路(三)
    Java学习之路(二)
    Java学习之路(一)
    UML类图几种关系的总结(转)
    vue 项目全局修改element-ui的样式
    NGINX 资料
  • 原文地址:https://www.cnblogs.com/tianzhiyun/p/9471087.html
Copyright © 2011-2022 走看看