Java笔记1 : 在生产者消费者模式中,线程通信与共享数据,死锁问题与解决办法

    本例定义了4个类,这里说一下,方便下面讲解。分别是Product(产品),Producer(生产者),Consumer(消费者), Test(测试类)。

多线程之间通信与共享数据只要引用同一内存区域就可以了,做法就是new一个对象,传多个引用。

 

1 Product pro = new Product();
2 Producer producer = new Producer(pro);
3 Consumer consumer = new Consumer(pro);

 

    但是由于cpu的随机性,共享数据时容易出现非法数据,这个不必多说了。解决办法就是线程同步,在一个线程对共享的数据访问完之前,不允许另一个线程访问。

    但是如果代码写成下面这样,则会出现死锁问题。(虽然本例使用的循环队列容量比较大,一般不会出现,不过这确实是安全隐患)

 1 public class Product {
 2     
 3     private boolean[] pro = new boolean[100];
 4     private int head = 0;
 5     private int rear = 0;
 6     
 7     public synchronized void production() {
 8         while((rear + 1) % 100 == head) {
 9             try {
10                 this.wait();
11             } catch (InterruptedException e) {
12                 e.printStackTrace();
13             }
14         }
15         System.out.println("生产了一件产品, 放在位置 : " + rear);
16         pro[rear] = true;
17         rear = (rear + 1) % 100;
18         this.notify();
19     }
20     
21     public synchronized void consume() {
22         while(rear == head) {
23             try {
24                 this.wait();
25             } catch (InterruptedException e) {
26                 e.printStackTrace();
27             }
28         }
29         System.out.println("消费了一件产品, 来自位置 : " + head);
30         pro[head] = false;
31         head = (head + 1) % 100;
32         this.notify();
33     }
34 }

    上面代码判断时用的while而不是if(同步代码中使用while代替if是一种技巧),这样避免判断完成之后cpu切换到另一线程,切换回来时由于没有再次判断,容易造成非法数据的问题。

    解决死锁的第一种方案是使用synchronizedobject中的各种方法,需要notifyAll代替notify,避免当队列已满且消费者全都wait时,生产者无法生产,造成死锁问题;第二种方案是使用Lock接口和Condition接口,可以用与第一种同样的方法,还可以利用Condition的多监视器绑定的特性,为生产者和消费者分别设置不同的监视器,这样保证生产者唤醒消费者,消费者唤醒生产者,就不会出现死锁问题了。

给出解决后的代码:

 1 public class Product {
 2     
 3     private boolean[] pro = new boolean[100];
 4     private int head = 0;
 5     private int rear = 0;
 6     
 7     public synchronized void production() {
 8         while((rear + 1) % 100 == head) {
 9             try {
10                 this.wait();
11             } catch (InterruptedException e) {
12                 e.printStackTrace();
13             }
14         }
15         System.out.println("生产了一件产品, 放在位置 : " + rear);
16         pro[rear] = true;
17         rear = (rear + 1) % 100;
18         this.notifyAll();
19     }
20     
21     public synchronized void consume() {
22         while(rear == head) {
23             try {
24                 this.wait();
25             } catch (InterruptedException e) {
26                 e.printStackTrace();
27             }
28         }
29         System.out.println("消费了一件产品, 来自位置 : " + head);
30         pro[head] = false;
31         head = (head + 1) % 100;
32         this.notifyAll();
33     }
34 }
 1 import java.util.concurrent.locks.Condition;
 2 import java.util.concurrent.locks.Lock;
 3 import java.util.concurrent.locks.ReentrantLock;
 4 
 5 public class Product2 {
 6     
 7     private boolean[] pro = new boolean[100];
 8     private int head = 0;
 9     private int rear = 0;
10     Lock lock = new ReentrantLock();
11     Condition production_con = lock.newCondition();
12     Condition consume_con = lock.newCondition();
13     
14     public void production() {
15         lock.lock();
16         while((rear + 1) % 100 == head) {
17             try {
18                 production_con.await();
19             } catch (InterruptedException e) {
20                 e.printStackTrace();
21             }
22         }
23         try {
24             System.out.println("生产了一件产品, 放在位置 : " + rear);
25             pro[rear] = true;
26             rear = (rear + 1) % 100;
27             consume_con.signalAll();
28         } finally {
29             lock.unlock();
30         }
31     }
32     
33     public void consume() {
34         lock.lock();
35         while(rear == head) {
36             try {
37                 consume_con.await();
38             } catch (InterruptedException e) {
39                 e.printStackTrace();
40             }
41         }
42         try {
43             System.out.println("消费了一件产品, 来自位置 : " + head);
44             pro[head] = false;
45             head = (head + 1) % 100;
46             production_con.signalAll();
47         } finally {
48             lock.unlock();
49         }
50     }
51 }

最后贴上Producer,Consumer,Test三个类的代码

 1 public class Producer implements Runnable {
 2     private Product pro;
 3     public Producer(Product pro) {
 4         this.pro = pro;
 5     }
 6     public void run() {
 7         for(int i = 0; i < 100; i++) {
 8             pro.production();
 9             try {
10                 Thread.sleep(100);
11             } catch (InterruptedException e) {
12                 e.printStackTrace();
13             }
14         }
15     }
16 }
 1 public class Consumer implements Runnable {
 2     private Product pro;
 3     public Consumer(Product pro) {
 4         this.pro = pro;
 5     }
 6     public void run() {
 7         for(int i = 0; i < 100; i++) {
 8             pro.consume();
 9             try {
10                 Thread.sleep(100);
11             } catch (InterruptedException e) {
12                 e.printStackTrace();
13             }
14         }
15     }
16 }
 1 public class Test {
 2     
 3     public static void main(String[] args) {
 4         //为了方便阅读,没有用匿名类
 5         Product pro = new Product();
 6         Producer producer = new Producer(pro);
 7         Consumer consumer = new Consumer(pro);
 8         Thread p1 = new Thread(producer);
 9         Thread p2 = new Thread(producer);
10         Thread c1 = new Thread(consumer);
11         Thread c2 = new Thread(consumer);
12         p1.start();
13         p2.start();
14         c1.start();
15         c2.start();
16     }
17 }

 

 

posted @ 2013-12-26 18:39  Anti-Magic  阅读(299)  评论(0编辑  收藏  举报