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();
            
        }

    }

  • 相关阅读:
    使用事件驱动模型实现高效稳定的网络服务器程序
    Muduo 多线程模型:一个 Sudoku 服务器演变
    淘宝李晓拴:淘宝网PHP电子商务应用
    又一随机视频聊天网站内侧了,名字叫QQ偶遇,地址为:http://qq.17ouyu.com/
    又一随机视频聊天网站内侧了,地址为:http://www.17ouyu.com/
    多线程服务器的常用编程模型
    Linux系统shell脚本对字符串、数字、文件的判断
    【2022.01.11】HassOS的备份、HassOS的SSH连接、Docker的部署
    【2022.01.09】了解HassOS的开发者工具——添加自定义组件
    【2022.01.10】装修别墅、洋房户型的一些注意事项
  • 原文地址:https://www.cnblogs.com/zzzhangzheng/p/6127998.html
Copyright © 2011-2022 走看看