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

    }

  • 相关阅读:
    第一天 学习绪论
    我的java学习道路
    windows2003 iis+dedecms 4.0701版本,登录后台显示空白
    iis+php+mysql
    net 连mysql奇怪问题
    Windows 2008安装SQL 2008图解
    注册码
    [VS2013]如何闪开安装VS2013必须要有安装IE10的限制
    net SqlBulkCopy拷贝数据的问题
    Android应用与系统安全防御
  • 原文地址:https://www.cnblogs.com/zzzhangzheng/p/6127998.html
Copyright © 2020-2023  润新知