62,634
社区成员




01 class P
02 {
03 private String name="李四";
04 private String sex="女";
05 boolean bFull = false ;
06 public synchronized void set(String name,String sex)
07 {
08 if(bFull)
09 {
10 try
11 {
12 wait() ; // 后来的线程要等待
13 }
14 catch(InterruptedException e)
15 {}
16 }
17 this.name = name ;
18 try
19 {
20 Thread.sleep(10);
21 }
22 catch(Exception e)
23 {
24 System.out.println(e.getMessage());
25 }
26 this.sex = sex ;
27 bFull = true ;
28 notify(); // 唤醒最先到达的线程
29 }
30 public synchronized void get()
31 {
32 if(!bFull)
33 {
34 try
35 {
36 wait() ;
37 }
38 catch(InterruptedException e)
39 {}
40 }
41 System.out.println(name+" ---->"+sex);
42 bFull = false ;
43 notify();
44 }
45 }