(转)Java多线程技术 -- 等待唤醒机制(一)
原博客网站:http://blog.csdn.net/watermusicyes/article/details/8804530
在开始讲解等待唤醒机制之前,有必要搞清一个概念 —— 线程之间的通信:
多个线程在处理同一个资源,但是处理的动作(线程的任务)却不相同。通过一定的手段使各个线程能有效的利用资源。而这种手段即—— 等待唤醒机制。
等待唤醒机制所涉及到的方法:
wait() :等待,将正在执行的线程释放其执行资格 和 执行权,并存储到线程池中。
notify():唤醒,唤醒线程池中被wait()的线程,一次唤醒一个,而且是任意的。
notifyAll(): 唤醒全部:可以将线程池中的所有wait() 线程都唤醒。
其实,所谓唤醒的意思就是让 线程池中的线程具备执行资格。必须注意的是,这些方法都是在 同步中才有效。同时这些方法在使用时必须标明所属锁,这样才可以明确出这些方法操作的到底是哪个锁上的线程。
仔细查看JavaAPI之后,发现这些方法 并不定义在 Thread中,也没定义在Runnable接口中,却被定义在了Object类中,为什么这些操作线程的方法定义在Object类中?
因为这些方法在使用时,必须要标明所属的锁,而锁又可以是任意对象。能被任意对象调用的方法一定定义在Object类中。
接下里,我们先从一个简单的示例入手:

如上图说示,输入线程 想Resource中输入name ,sex , 输出线程从资源中输出,先要完成的任务是:
1. 当input发现Resource中没有数据时,开始输入,输入完成后,叫output来输出。如果发现有数据,就wait();
2. 当output发现Resource中没有数据时,就wait() ;当发现有数据时,就输出,然后,叫醒input来输入数据。
这个问题的关键在于,如何来做到 交替进行。
下面看具体的代码实现:
1 public class Resource { 2 private String name ; 3 private String sex; 4 private boolean flag = false; 5 6 public synchronized void set(String name , String sex){ 7 8 if(flag) 9 try { 10 wait(); 11 } catch (InterruptedException e) { 12 // TODO Auto-generated catch block 13 e.printStackTrace(); 14 } 15 //设置成员变量 16 this.name = name; 17 this.sex = sex; 18 //设置之后,Resource中有值,将标记该为 true , 19 flag = true; 20 //唤醒output 21 this.notify(); 22 } 23 public synchronized void out(){ 24 if(!flag) 25 try { 26 wait(); 27 } catch (InterruptedException e) { 28 // TODO Auto-generated catch block 29 e.printStackTrace(); 30 } 31 //输出线程将数据输出 32 System.out.println("The name is : " + name + " && The sex is : " + sex); 33 //改变标记,以便输入线程输入数据 34 flag = false; 35 //唤醒input,进行数据输入 36 this.notify(); 37 } 38 39 } 40 41 public class Input implements Runnable { 42 43 private Resource r; 44 public Input(Resource r){ 45 this.r = r; 46 } 47 48 @Override 49 public void run() { 50 int count = 0 ; 51 while(true){ 52 if(count == 0){ 53 r.set("Tom", "man"); 54 }else{ 55 r.set("Lily", "woman"); 56 } 57 //在连个数据之间进行切换。 58 count = (count + 1)%2; 59 } 60 } 61 62 } 63 64 public class Output implements Runnable { 65 66 private Resource r ; 67 public Output(Resource r ){ 68 this.r = r; 69 } 70 @Override 71 public void run() { 72 while(true){ 73 r.out(); 74 } 75 76 } 77 } 78 79 public class ResourceDemo { 80 81 public static void main(String[] args) { 82 //资源对象 83 Resource r = new Resource(); 84 //任务对象 85 Input in = new Input(r); 86 Output out = new Output(r); 87 //线程对象 88 Thread t1 = new Thread(in); 89 Thread t2 = new Thread(out); 90 //开启线程 91 t1.start(); 92 t2.start(); 93 } 94 }
在这个例子中,我们应用了等待唤醒机制,这是最简单的应用,下面我们来看看等待唤醒机制的经典问题:生产者,消费者问题!
当然也是,从最简单的问题开始看起:即单个消费者,生产者。这个例子和上个例子极其相似,因此不再赘述。接下来,主要谈一下,多生产者,多消费者的情形。
看下例子代码:
1 public class Resource { 2 private String name; 3 private int count = 1; 4 private boolean flag ; 5 6 public synchronized void set(String name){ 7 if(flag){ 8 try { 9 this.wait(); 10 } catch (InterruptedException e) { 11 // TODO Auto-generated catch block 12 e.printStackTrace(); 13 } 14 } 15 this.name = name + count; 16 count++; 17 18 System.out.println(Thread.currentThread().getName() 19 + "The producer name is :++++++++++++++ " + this.name); 20 21 flag = true; 22 23 this.notify(); 24 25 } 26 27 public synchronized void out(){ 28 if(!flag){ 29 try { 30 this.wait(); 31 } catch (InterruptedException e) { 32 // TODO Auto-generated catch block 33 e.printStackTrace(); 34 } 35 } 36 System.out.println(Thread.currentThread().getName() + 37 "The consumer name is : ---------------------------" + this.name); 38 flag = false; 39 40 this.notify(); 41 42 } 43 44 } 45 46 public class Producer implements Runnable { 47 48 private Resource r; 49 public Producer(Resource r){ 50 this.r = r; 51 } 52 @Override 53 public void run() { 54 55 while(true){ 56 r.set("馒头"); 57 } 58 59 } 60 } 61 62 public class Consumer implements Runnable { 63 64 private Resource r; 65 public Consumer(Resource r ){ 66 this.r = r; 67 } 68 @Override 69 public void run() { 70 71 while(true){ 72 r.out(); 73 } 74 } 75 } 76 77 public class ProConDemo { 78 79 public static void main(String[] args) { 80 81 //资源对象 82 Resource r = new Resource(); 83 //任务对象 84 Producer pro = new Producer(r); 85 Consumer con = new Consumer(r); 86 //线程对象,两个生产者,两个消费者 87 Thread t1 = new Thread(pro); 88 Thread t2 = new Thread(pro); 89 90 Thread t3 = new Thread(con); 91 Thread t4 = new Thread(con); 92 93 //开启线程 94 t1.start(); 95 t2.start(); 96 t3.start(); 97 t4.start(); 98 } 99 100 }
但是经过运行,我们会发现这样的问题:

发现:生产者生产的商品没有被消费就生产了新的商品。
经过分析,发现,产生这种状况的根源是:
1,本方唤醒了本方。
2,被唤醒的本方没有判断标记。
为此,我们要做的改进是——将if 判断改为 while 标记,保证,每次被wait的线程在醒了之后,都得再次判断标记。进过修改,再次运行之后,会发现新的问题又来了,产生了死锁。经过分析,这个问题的根源在于,本方唤醒了本方,被唤醒的本方判断标记后,发现,标记不成立,就继续等待,因此,再也没有活着的线程(while+notify会导致死锁)。为此,我们需要唤醒,所有的线程,就用到了 notifyAll() 。将线程池中,所有等待的线程都叫醒。
经过修改后的代码成功的解决了上述的问题。
修改后的代码为:
1 public class Resource { 2 private String name; 3 private int count = 1; 4 private boolean flag ; 5 6 public synchronized void set(String name){ 7 //用while 确保线程醒了后再次,判断标记。 8 while(flag){ 9 try { 10 this.wait(); 11 } catch (InterruptedException e) { 12 // TODO Auto-generated catch block 13 e.printStackTrace(); 14 } 15 } 16 this.name = name + count; 17 count++; 18 19 System.out.println(Thread.currentThread().getName() 20 + "The producer name is :++++++++++++++ " + this.name); 21 22 flag = true; 23 //唤醒所有被等待的线程 24 this.notifyAll(); 25 26 } 27 28 public synchronized void out(){ 29 //用while 确保线程醒了后再次,判断标记。 30 while(!flag){ 31 try { 32 this.wait(); 33 } catch (InterruptedException e) { 34 // TODO Auto-generated catch block 35 e.printStackTrace(); 36 } 37 } 38 System.out.println(Thread.currentThread().getName() + 39 "The consumer name is : ---------------------------" + this.name); 40 flag = false; 41 //唤醒所有被等待的线程 42 this.notifyAll(); 43 44 } 45 }

浙公网安备 33010602011771号