多线程的Wait返回它等待的下方_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 多线程的Wait返回它等待的下方

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

 2014/8/15 15:34:44  xmind  程序员俱乐部  我要评论(0)
  • 摘要:多线程的Wait返回它等待的下方。JavaCode123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869packageorg.vocano.java.tst;classThreadDemo{publicstaticvoidmain(String[]args)
  • 标签:多线程 线程
  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!

    ?

发表评论
用户名: 匿名