要求是这个,作业1:有一个水池,水池的容量是固定 的500L,一边为进水口,一边为出水口.要求,进水与放水不能同时进行,水池一旦满了不能继续注水,一旦放空了,不可以继续放水. 进水的速度5L/s , 放水的速度2L/s 
class Pool{
int capacity = 500;
int now = 0;
boolean flag = false;
}class Retain extends Thread{
int intlet = 5;
Pool p;
public Retain(Pool p){
this.p = p;
}
public void run(){
while(true){
synchronized (p) {
if(p.flag==false){
if((p.now+intlet)<=p.capacity){
p.now+=intlet;
System.out.println("现在水量:"+p.now);
}else{
try {
p.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
p.notify();
p.flag =true;

}
}
}
}class Drain extends Thread{
int drain = 2;
Pool p;
public Drain(Pool p){
this.p = p;
}
public void run(){
while(true){
synchronized (p) {
if(p.flag==true){
if((p.capacity-drain)>=0){
p.capacity-=drain;
System.out.println("正在排水:"+p.capacity);
}else{
try {
p.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
p.notify();
p.flag = false;

}
}
}
}public class TestPool { public static void main(String[] args) {
Pool a = new Pool();
Retain retain = new Retain(a);
Drain drain = new Drain(a);
retain.start();
drain.start();
// TODO Auto-generated method stub }}上面是我自己写的,
输出结果是
现在水量:5
正在排水:498
现在水量:10
正在排水:496
现在水量:15
正在排水:494
现在水量:20
正在排水:492
现在水量:25
正在排水:490
现在水量:30
正在排水:488
现在水量:35
正在排水:486
现在水量:40
正在排水:484
现在水量:45
正在排水:482
现在水量:50
正在排水:480
现在水量:55
正在排水:478
现在水量:60
正在排水:476
现在水量:65
正在排水:474
现在水量:70
正在排水:472
现在水量:75
正在排水:470
现在水量:80
正在排水:468
现在水量:85
正在排水:466
现在水量:90
正在排水:464
现在水量:95
正在排水:462
现在水量:100
正在排水:460
现在水量:105
正在排水:458
现在水量:110
正在排水:456
现在水量:115
正在排水:454
现在水量:120
正在排水:452
现在水量:125
正在排水:450
现在水量:130
正在排水:448
现在水量:135
正在排水:446
现在水量:140
正在排水:444
现在水量:145
正在排水:442
现在水量:150
正在排水:440
现在水量:155
正在排水:438
现在水量:160
正在排水:436
现在水量:165
正在排水:434
现在水量:170
正在排水:432
现在水量:175
正在排水:430
现在水量:180
正在排水:428
现在水量:185
正在排水:426
现在水量:190
正在排水:424
现在水量:195
正在排水:422
现在水量:200
正在排水:420
现在水量:205
正在排水:418
现在水量:210
正在排水:416
现在水量:215
正在排水:414
现在水量:220
正在排水:412
现在水量:225
正在排水:410
现在水量:230
正在排水:408
现在水量:235
正在排水:406
现在水量:240
正在排水:404
现在水量:245
正在排水:402
现在水量:250
正在排水:400
现在水量:255
正在排水:398
现在水量:260
正在排水:396
现在水量:265
正在排水:394
现在水量:270
正在排水:392
现在水量:275
正在排水:390
现在水量:280
正在排水:388
现在水量:285
正在排水:386
现在水量:290
正在排水:384
现在水量:295
正在排水:382
现在水量:300
正在排水:380
现在水量:305
正在排水:378
现在水量:310
正在排水:376
现在水量:315
正在排水:374
现在水量:320
正在排水:372
现在水量:325
正在排水:370
现在水量:330
正在排水:368
现在水量:335
正在排水:366
现在水量:340
正在排水:364
现在水量:345
正在排水:362
现在水量:350
正在排水:360
现在水量:355
正在排水:358
正在排水:356
正在排水:354
正在排水:352
正在排水:350
正在排水:348
正在排水:346
正在排水:344
正在排水:342
正在排水:340
正在排水:338
正在排水:336
正在排水:334
正在排水:332
正在排水:330
正在排水:328
正在排水:326
正在排水:324
正在排水:322
正在排水:320
正在排水:318
正在排水:316
正在排水:314
正在排水:312
正在排水:310
正在排水:308
正在排水:306
正在排水:304
正在排水:302
正在排水:300
正在排水:298
正在排水:296
正在排水:294
正在排水:292
正在排水:290
正在排水:288
正在排水:286
正在排水:284
正在排水:282
正在排水:280
正在排水:278
正在排水:276
正在排水:274
正在排水:272
正在排水:270
正在排水:268
正在排水:266
正在排水:264
正在排水:262
正在排水:260
正在排水:258
正在排水:256
正在排水:254
正在排水:252
正在排水:250
正在排水:248
正在排水:246
正在排水:244
正在排水:242
正在排水:240
正在排水:238
正在排水:236
正在排水:234
正在排水:232
正在排水:230
正在排水:228
正在排水:226
正在排水:224
正在排水:222
正在排水:220
正在排水:218
正在排水:216
正在排水:214
正在排水:212
正在排水:210
正在排水:208
正在排水:206
正在排水:204
正在排水:202
正在排水:200
正在排水:198
正在排水:196
正在排水:194
正在排水:192
正在排水:190
正在排水:188
正在排水:186
正在排水:184
正在排水:182
正在排水:180
正在排水:178
正在排水:176
正在排水:174
正在排水:172
正在排水:170
正在排水:168
正在排水:166
正在排水:164
正在排水:162
正在排水:160
正在排水:158
正在排水:156
正在排水:154
正在排水:152
正在排水:150
正在排水:148
正在排水:146
正在排水:144
正在排水:142
正在排水:140
正在排水:138
正在排水:136
正在排水:134
正在排水:132
正在排水:130
正在排水:128
正在排水:126
正在排水:124
正在排水:122
正在排水:120
正在排水:118
正在排水:116
正在排水:114
正在排水:112
正在排水:110
正在排水:108
正在排水:106
正在排水:104
正在排水:102
正在排水:100
正在排水:98
正在排水:96
正在排水:94
正在排水:92
正在排水:90
正在排水:88
正在排水:86
正在排水:84
正在排水:82
正在排水:80
正在排水:78
正在排水:76
正在排水:74
正在排水:72
正在排水:70
正在排水:68
正在排水:66
正在排水:64
正在排水:62
正在排水:60
正在排水:58
正在排水:56
正在排水:54
正在排水:52
正在排水:50
正在排水:48
正在排水:46
正在排水:44
正在排水:42
正在排水:40
正在排水:38
正在排水:36
正在排水:34
正在排水:32
正在排水:30
正在排水:28
正在排水:26
正在排水:24
正在排水:22
正在排水:20
正在排水:18
正在排水:16
正在排水:14
正在排水:12
正在排水:10
正在排水:8
正在排水:6
正在排水:4
正在排水:2
正在排水:0
按题目要求应该是,水不满的情况,蓄水到500,然后排水进程开始排水,排到0了又开始蓄水到500,。
自己是按这个想法写的,但是发现不能这样实现,不知道问题在哪里。
求教了。

解决方案 »

  1.   


    public class Pool { private int volumn;

    public Pool(){
    volumn = 0;
    }

    public void intake(int a) throws InterruptedException{
    volumn = volumn + a;
    System.out.println(a + " liters are injected.");
    notifyAll();
    }

    public void release(int b) throws InterruptedException{
    volumn = volumn - b;
    System.out.println(b + " liters are released.");
    }

    public int getVolumn(){
    return this.volumn;
    }
    }public class InjectPipe implements Runnable{ private Pool pool;

    public InjectPipe(Pool p){
    this.pool = p;
    }

    public void run(){
    synchronized(pool){
    try{
    while(true){
    if(pool.getVolumn() < 495){
    pool.intake(5);
    pool.notifyAll();
    Thread.sleep(1000);
    }
    System.out.println("Pool has " + pool.getVolumn() + " Liters after inject");
    pool.wait(); }
    }catch(InterruptedException ie){
    ie.printStackTrace();
    }
    }
    }
    }public class OutletPipe implements Runnable{ private Pool pool;

    public OutletPipe(Pool p){
    this.pool = p;
    }

    public void run(){
    synchronized(pool){
    try{
    while(true){
    if(pool.getVolumn() > 2){
    Thread.sleep(1000);
    pool.release(2);
    pool.notifyAll();
    }
    System.out.println("Pool has " + pool.getVolumn() + " Liters after release.");
    pool.wait();
    }
    }catch(InterruptedException ie){
    ie.printStackTrace();
    }
    }
    }
    }public class PoolTest { public static void main(String[] args) {
    // TODO Auto-generated method stub
    Pool pool = new Pool();
    Thread out = new Thread(new OutletPipe(pool));
    Thread in = new Thread(new InjectPipe(pool));

    in.start();
    out.start();
    }}
      

  2.   

    这是出入水管不同时开的代码
    public class OutletPipe implements Runnable{ private Pool pool;

    public OutletPipe(Pool p){
    this.pool = p;
    }

    public void run(){
    synchronized(pool){
    try{
    while(true){
    if(pool.getVolumn() < 2){
    pool.wait();
    }
    System.out.println("Pool has " + pool.getVolumn() + " Liters after release.");
    Thread.sleep(100);
    pool.release(2);
    pool.notifyAll();
    }
    }catch(InterruptedException ie){
    ie.printStackTrace();
    }
    }
    }
    }public class InjectPipe implements Runnable{ private Pool pool;

    public InjectPipe(Pool p){
    this.pool = p;
    }

    public void run(){
    synchronized(pool){
    try{
    while(true){
    if(pool.getVolumn() > 495){
    pool.wait();
    }
    System.out.println("Pool has " + pool.getVolumn() + " Liters after inject"); pool.intake(5);
    pool.notifyAll();
    Thread.sleep(100);
    }
    }catch(InterruptedException ie){
    ie.printStackTrace();
    }
    }
    }
    }
      

  3.   

    代码的逻辑有点问题,我改了下你说的功能实现了,你看看:
    public class Retain extends Thread{
     int intlet = 5;
        Pool p;
        public Retain(Pool p){
            this.p = p;
        }
        public void run(){       
            while(true){
             synchronized (p) {   
                        if(p.flag==false){
                            if((p.now+intlet)<=p.capacity){
                                p.now+=intlet;
                                System.out.println("现在水量:"+p.now);
                            }else{                           
                                try {
                                 p.flag =true;
                                 p.notify();
                                    p.wait();
                                      
                                } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();                               
                                }                       
                            }                       
                        }
                                        
             }                            
                
            }
        }
    }public class Drain extends Thread{
    int drain = 2;
        Pool p;
        public Drain(Pool p){
            this.p = p;
        }
        public void run(){
            while(true){           
                synchronized (p) {
                  if(p.flag==true){
                        if((p.now-drain)>=0){
                           p.now-=drain;
                           System.out.println("正在排水:"+p.now);
                        }else{                       
                            try {
                             p.flag =false;
                             p.notify();
                             p.wait();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }                       
                        }                                                               
                  }
                            
                         
                }       
            }
        }
    }
      

  4.   

    你一个对now操作,一个对capacity 操作干什么?capacity我没理解错的话是总容量吧!可以改吗?
      

  5.   

    package test;import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    public class ThreadTest { public static void main(String[] args) {
    new inWater().start();
    new outWater().start(); }}class inWater extends Thread{
    public void run(){
    while(true){
    Pool in = Pool.getInstance();
    in.inWater();
    }
    }
    }class outWater extends Thread{
    public void run(){
    while(true){
    Pool out = Pool.getInstance();
    out.outWater();
    }
    }
    }
    class Pool {
    private int poolsize  = 500; //水池大小
    private int water = 0;      //水池现有水量
    private static Pool  instance = null;

    private Lock lock = new ReentrantLock();  

    private Condition  inLock = lock.newCondition();

    private Condition  outLock = lock.newCondition();

    public static synchronized Pool getInstance(){
    if(instance ==null){
    instance = new Pool();
    }
    return instance;
    } public  void outWater() {
    lock.lock();
    try{
    while(water - 2 < 0){
    outLock.await();
    }
    water = water - 2 ;
    inLock.signal();
    System.out.println("正在出水,现在水量为:"+water);
    }catch(Exception e){
    System.out.println("out error!"+e);
    }finally{
    lock.unlock();
    }

    } public void inWater() {
    lock.lock();
    try{
    while(water + 5 > poolsize){
    inLock.await();
    }
    water = water + 5 ;
    outLock.signal();
    System.out.println("正在进水,现在水量为:"+water);
    }catch(Exception e){
    System.out.println("in error!"+e);
    }finally{
    lock.unlock();
    }

    }
    }
      

  6.   


    恩,我后来发现了改了,还是不行,我逻辑出问题,楼上@soton_dolphin那样是对的,但是我想不到
      

  7.   


    你想太多了,这个作业只不过是想练习 wait() 和notify() 还有synchronized的用法。
      

  8.   

    class Pool
    {
    static int volume;
    String name;
    boolean s = false;
    static int flag;
    }  
    class Addwater extends Thread
    {
    Pool p;
        //double velocity = 5;
    public Addwater(Pool p)
    {
        this.p = p;
    }
    @Override
    public void run()
    {
      //int volume = 0;
    while(true)
      {
       synchronized (p)
       {
    if (p.s == false)
    {
      if (p.volume <= 495)
           {
    System.out.println("现在水池的水为"+p.volume);
    p.volume = p.volume + 5 ;
    System.out.println("加水后水池的水为"+p.volume);        }
      else
       {
        System.out.println("水池已满不能再加");
    break;
      
       }
    p.s = true;//更改标识
    p.notify();//唤醒防水池 
    }
     else
    {
          try
          {
    p.wait();
          }
          catch (InterruptedException e)
          {
      e.printStackTrace();
          }
    }
     
           }
                     
       } }
    }
    class Drawwater extends Thread
    {
    Pool p;
        public Drawwater(Pool p)
       {
          this.p = p;
       }
    @Override
        public void run()
    {
       while(true)
       {
        synchronized (p)
     {
      if (p.s == true)
       {
     
      //System.out.println("开始放水");
                      p.volume = p.volume - 2;
      System.out.println("放水后现在pool里面的水量为"+p.volume);
      p.s = false;
      p.notify();
    }  
         else
    {
        //System.out.println("pool内部无水");
        try
    {
    p.wait();
    }
    catch (InterruptedException e)
    {
     e.printStackTrace();


        }

       
       
              }  
    }   
    }   
    }   
    class Getpool 
    {
    public static void main(String[] args)
    {
       Pool p = new Pool();
       Addwater a = new Addwater(p);
           Drawwater b = new Drawwater(p);
       a.start();
       b.start();

    }
    }
      

  9.   

    好吧,既然就是为了练习线程同步,上个为了同步而同步的代码:
    public class Pool2{
    public int poolSize;//水池容量
    public int waterSize;//当前水池水量
    public int inWaterSize;//进水量
    public int outWaterSize;//出水量

    public synchronized void inWater() throws Exception{
    while(true){
    if((poolSize - waterSize) >= inWaterSize){
    System.out.println("当前水池容量为" + waterSize + ",继续进水。");
    waterSize += inWaterSize;
    Thread.sleep(200);//模拟进水的时间
    }else{
    System.out.println("水池容量已达上限" + waterSize + ",开始放水。");
    this.notify();
    this.wait();
    }
    }
    }

    public synchronized void outWater() throws Exception{
    while(true){
    if(waterSize >= outWaterSize){
    System.out.println("当前水池容量为" + waterSize + ",继续放水。");
    waterSize -= outWaterSize;
    Thread.sleep(200);//模拟放水的时间
    }else{
    System.out.println("水池容量已达下限" + waterSize + ",开始进水。");
    this.notify();
    this.wait();
    }
    }
    }

    public static void main(String[] args) {
    final Pool2 pool  = new Pool2();
    pool.poolSize = 20;
    pool.waterSize = 0;
    pool.inWaterSize = 5;
    pool.outWaterSize = 2;

    new Thread(new Runnable(){
    @Override
    public void run(){
    try {
    pool.inWater();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }).start();
    new Thread(new Runnable(){
    @Override
    public void run(){
    try {
    pool.outWater();
    } catch (Exception e) {
    e.printStackTrace();
    };
    }
    }).start();
    }
    }
      

  10.   

    package rong.guo05;public class Demo {
    public static void main(String[] args){
    Poor p = new Poor();

    Into i = new Into(p);
    Outer o = new Outer(p);

    Thread t1 = new Thread(i);
    Thread t2 = new Thread(o);
    t1.start();
    t2.start();
    }}
    package rong.guo05;public class Poor {
    private int volume = 0;//水池默认水量为0
    static final int VOLUME_MAX = 500;

    public Poor(){}

    public int getVolume(){
    return volume;
    }

    //注水方法
    public void inWater(){
    volume += 5;
    }

    //放水方法
    public  void outWater(){
    volume -= 2;
    }
    }
    package rong.guo05;public class Into implements Runnable {
    Poor p; public Into(Poor p) {
    this.p = p;
    } @Override
    public void run() {
    while (true) { synchronized (p) {
    if ((p.getVolume() + 5) < p.VOLUME_MAX) { // 判断水池的水量是否超过容量500L
    p.inWater();
    System.out.println("注水中。。水量为:" + p.getVolume());
    try {
    Thread.sleep(100);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    p.notify();
    } else {
    try {
    p.wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    } } } }}
    [code=java]package rong.guo05;public class Outer implements Runnable {
    private Poor p; public Outer(Poor p) {
    this.p = p;
    } @Override
    public void run() {
    while (true) { synchronized (p) {
    if ((p.getVolume() - 2) > 0) {
    p.outWater();
    System.out.println("出水中、、、水量为:" + p.getVolume());
    try {
    Thread.sleep(100);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    p.notify();
    } else {
    try {
    p.wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    } }
    } }}
    [/code]
      

  11.   


    你想太多了,这个作业只不过是想练习 wait() 和notify() 还有synchronized的用法。我不认为他想多了。
    1、一边放水一边进水很明显用多线程模拟
    2、进水和放水不能同时进行指的是在一次进水过程中不可以放水,就是进水线程在实现进水方法的过程中放水线程是不可以对池子进行操作的,反之亦然。所以要用synchronized
    3、并没有要求放完或者注满才能进水或放水,这就是生产者和消费者的问题。当放空和注满的时候运用wait()和notify()控制线程。这个题就是考线程和线程同步的理解和运用。