wait
notify ()
nitifyAll ()
都使用在同步中,因为要对持有监视器(锁)的线程操作
所以要使用在同步中,因为只有同步才具有锁
为什么这些操作线程的方法要定义object类中呢
因为这些方法在操作同步中线程时。都必须要标识他们所操作线程只有的锁
只有同一个锁上的被等待线程,可以被同一个锁上的notify唤醒
不可以对不同锁中的线程进行唤醒
也就是说,等待和唤醒必须是同一个锁
而锁可以使任意对象,所以可以被任意对象调用的方法定义object类中
1 class Res 2 { 3 String name; 4 String sex; 5 boolean flag = false; 6 } 7 8 class Input implements Runnable 9 { 10 private Res r ; 11 Input (Res r){ 12 this.r = r; 13 } 14 public void run (){ 15 int x = 0; 16 while (true) 17 { 18 synchronized (r) { 19 if (r.flag) 20 try {r.wait();}catch(Exception e){} 21 if (x==0){ 22 r.name = "java"; 23 r.sex = "man"; 24 } 25 else 26 { 27 r.name = "杰克"; 28 r.sex = "女女vn"; 29 30 } 31 x = (x+1)%2; 32 r.flag = true; 33 r.notify(); 34 35 } 36 } 37 38 } 39 40 } 41 42 43 44 class Output implements Runnable 45 { 46 private Res r; 47 Output (Res r){ 48 49 this.r = r; 50 } 51 public void run (){ 52 while (true){ 53 synchronized(r){ 54 55 if (!r.flag) 56 57 try {r.wait();}catch (Exception e){} 58 System.out.println (r.name+"..."+r.sex); 59 r.flag = false; 60 r.notify(); 61 62 } 63 } 64 65 } 66 67 } 68 69 public class InputOutputDemo { 70 71 public static void main(String[] args) { 72 // TODO Auto-generated method stub 73 74 System.out.println ("ddddd"); 75 Res r = new Res (); 76 Input input = new Input (r); 77 Output output = new Output (r); 78 79 80 Thread t1 = new Thread (input); 81 Thread t2 = new Thread (output); 82 t1.start(); 83 t2.start(); 84 } 85 86 }
在1.5之后出现了lock关键字因此使用这个关键字丰富和我们平时开发习惯
同时Condition 取代了wait notifly nitiflyAll
1 import java.util.concurrent.locks.ReentrantLock; 2 import java.util.concurrent.locks.Condition; 3 import java.util.concurrent.locks.Lock; 4 5 6 public class one { 7 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 System.out.println("ddd"); 11 RRRRRRR res = new RRRRRRR(); 12 13 Producer p1 = new Producer(res); 14 Producer p2 = new Producer(res); 15 16 consumers c1 = new consumers(res); 17 consumers c2 = new consumers(res); 18 19 Thread t1 = new Thread(p1); 20 Thread t2 = new Thread(p2); 21 Thread t3 = new Thread(c1); 22 Thread t4 = new Thread(c2); 23 24 t1.start(); 25 t2.start(); 26 t3.start(); 27 t4.start(); 28 } 29 30 } 31 class RRRRRRR 32 { 33 private String name; 34 private int count = 1; 35 private boolean flag = false; 36 37 private Lock lock = new ReentrantLock(); 38 private Condition condition_pro = lock.newCondition(); 39 private Condition condition_con = lock.newCondition(); 40 public void set (String name) throws InterruptedException{ 41 lock.lock(); 42 try{ 43 while (flag) 44 condition_pro.await(); 45 this .name = name + "--" + count++; 46 System.out.println(Thread.currentThread().getName()+"生产商品"+this.name); 47 flag = true; 48 condition_con.signal(); 49 } 50 finally{ 51 lock.unlock(); 52 } 53 } 54 55 56 57 public void out()throws InterruptedException { 58 lock.lock(); 59 try{ 60 while (!flag) 61 condition_con.await(); 62 System.out.println(Thread.currentThread().getName() + "消费了" + this.name); 63 flag = false; 64 condition_pro.signal(); 65 } 66 finally{ 67 lock.unlock(); 68 } 69 } 70 } 71 /* 72 * 生产者 73 */ 74 class Producer implements Runnable 75 { 76 private RRRRRRR res; 77 Producer (RRRRRRR res){ 78 79 this.res = res; 80 } 81 public void run(){ 82 83 try{ 84 while (true){ 85 86 res.set ("牙膏"); 87 } 88 } 89 catch(InterruptedException e){ 90 91 92 } 93 } 94 95 } 96 /* 97 * 消费者 98 */ 99 class consumers implements Runnable 100 { 101 102 private RRRRRRR res; 103 consumers(RRRRRRR res){ 104 105 this.res = res; 106 } 107 public void run(){ 108 try{ 109 110 while (true){ 111 res.out(); 112 } 113 } 114 catch(InterruptedException e){ 115 116 117 } 118 } 119 }