一个水池容量为1000个单位,水池每秒进水4个单位,每秒放水1个单位,求水池满了后的时间?

来源:互联网 发布:linux 目录介绍 编辑:程序博客网 时间:2024/04/26 04:57

package com.xxzzycq2.thread;public class Water{public static int count = 0;public static int time = 0;public static void main(String[] args){Thread t1 = new In();Thread t2 = new Out();t1.start();t2.start();}}class In extends Thread{public void run(){while(Water.count < 1000){synchronized (Water.class){try{Thread.sleep(1000);Water.count += 4;System.out.println(Water.count);Water.time++;if(Water.count >= 1000){System.out.println(Water.time);System.exit(0);}}catch (InterruptedException e){e.printStackTrace();}}}}}class Out extends Thread{public void run(){while(Water.count < 1000){synchronized (Water.class){try{Thread.sleep(1000);Water.count -= 1;System.out.println(Water.count);Water.time++;}catch (InterruptedException e){e.printStackTrace();}}}}}