多线程的Wait返回它等待的下方

  1. 多线程的Wait返回它等待的下方。
     Java Code 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
     
    package org.vocano.java.tst;
    class ThreadDemo {
         public  static  void main( String[] args) {
            Buffer buf =  new Buffer();
             new Producer(buf).start();
             new Consumer(buf).start();
             new Consumer(buf).start();
        }
    }
    class Producer  extends Thread {
         private Buffer buf;
         public Producer(Buffer buf) {
             this.buf = buf;
        }
         public  void run() {
             boolean odd = false;
             while(true) {
                 synchronized(buf) {
                    System.out.println( "Producer get lock!");
                     if(buf.bFull) {
                        System.out.println( "Producer wait!!!");
                         try{buf.wait();} catch(Exception e) {}
                        System.out.println( "Producer wait return return return !!!!!!! ");
                    }
                     if(odd) {
                        buf.name= "jack";    
                         try{Thread.sleep( 100);} catch(Exception e) {}
                        buf.sex= "female";
                    }  else {
                        buf.name= "lucy";    
                         try{Thread.sleep( 100);} catch(Exception e) {}
                        buf.sex= "male";
                    }
                    buf.bFull = true;
                    buf.notify();
                    System.out.println( "Producer release lock!");
                }
                odd = !odd;
            }
        }
    }
    class Consumer  extends Thread {
         private Buffer buf;
         public Consumer(Buffer buf) {
             this.buf = buf;
        }
         public  void run() {
             while(true) {
                 synchronized(buf) {
                    System.out.println( "Consumer get lock!");
                     if(!buf.bFull) {
                        System.out.println( "Consumer wait!!!");
                         try{buf.wait();} catch(Exception e) {}
                        System.out.println( "Consumer wait return return return !!!!!!! ");
                    }
                     try{Thread.sleep( 500);} catch(Exception e) {}
                    System.out.println(buf.name +  " : " + buf.sex);
                    buf.bFull = false;
                    buf.notify();
                    System.out.println( "Consumer release lock!");
                }
            }
        }
    }
    class Buffer {
         boolean bFull = false;
         String name =  "Unkown";
         String sex =  "Unkown";
    }
  2. 输出:可看出Wait的返回时另一个的wait或者release,不是自己getLock,说明notify放回wait的点。
     Java Code 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
     
    Producer get lock!
    Producer release lock!
    Consumer get lock!
    lucy : male
    Consumer release lock!
    Consumer get lock!
    Consumer wait!!!
    Producer get lock!
    Producer release lock!
    Producer get lock!
    Producer wait!!!
    Consumer wait  return  return  return !!!!!!! 
    jack : female
    Consumer release lock!
    Producer wait  return  return  return !!!!!!! 
    Producer release lock!
    Producer get lock!
    Producer wait!!!
    Consumer get lock!

     

你可能感兴趣的:(java,thread,wait)