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

    }

  • 相关阅读:
    椭圆形 上传图片预览 image preview (未整理版本)
    canvas学习笔记03:简单脉冲效果
    canvas学习笔记02:饼图&柱状图
    canvas学习笔记01:Math.sin & Math.cos
    java jdk 随机数阻塞问题
    家政/保洁 平台设计
    Lock和synchronized
    jvm 中java new 对象顺序
    Java 多线程原理
    java ftp上传下载,下载文件编码设置
  • 原文地址:https://www.cnblogs.com/zzzhangzheng/p/6127998.html
Copyright © 2011-2022 走看看