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();
}
}
}
}
}
一个水池容量为1000个单位,水池每秒进水4个单位,每秒放水1个单位,求水池满了后的时间?
最新推荐文章于 2021-05-06 20:26:01 发布
726

被折叠的 条评论
为什么被折叠?



