zoukankan      html  css  js  c++  java
  • java 简单的进水放水

    class Shui implements Runnable{
         int a=0;
        @Override
        
        public void run() {
            synchronized (this) {
            while (true) {
            String str= Thread.currentThread().getName();
                    
                    if (str.equals("进水")) {
                        if (a>496) {
                            this.notify();
                            try {
                                this.wait();
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                
                                e.printStackTrace();
                            }
                            
                            
                        }
                        a+=5;
                        System.out.println("每秒进水5升水池还有"+a);
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        
                    }
                    
                    if (str.equals("放水")) {
                        if (a<2) {
                            this.notify();
                            try {
                                this.wait();
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                
                                e.printStackTrace();
                            }
                            
                            
                        }
                        a-=2;
                        System.out.println("每秒放水2升水池还有"+a);
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    
                    }
                    
                    
                    
                }
            }
            
        }
        
    }
    public class work1 {
        
        
        public static void main(String[] args) {
            Shui sss=new Shui();
              Thread t = new Thread(sss,"进水");
              Thread t1 = new Thread(sss,"放水");
              t.start();
              t1.start();
            
        }

    }

  • 相关阅读:
    [FAQ] GitHub 开启二次验证之后,如何通过 https clone 项目 ?
    [FAQ] GoLand 需要手动开启代码补全吗 ?
    [FAQ] 夏玉米 按规则查询域名靠谱吗 ?
    [FAQ] Error: com.mysql.jdbc.Driver not loaded. :jdbc_driver_library
    [php-src] Php内核的有趣高频宏
    [php-src] Php扩展开发的琐碎注意点、细节
    [ELK] Docker 运行 Elastic Stack 支持 TLS 的两种简单方式
    [Contract] Solidity 生成随机数方案
    [MySQL] 导入数据库和表的两种方式
    [ELK] 生产环境中 Elasticsearch 的重要配置项
  • 原文地址:https://www.cnblogs.com/zzzhangzheng/p/6127998.html
Copyright © 2011-2022 走看看