您的位置:首页 > 编程语言 > Java开发

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

2013-12-26 18:39 891 查看
本例定义了4个类,这里说一下,方便下面讲解。分别是Product(产品),Producer(生产者),Consumer(消费者), Test(测试类)。

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

Product pro = new Product();
Producer producer = new Producer(pro);
Consumer consumer = new Consumer(pro);


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

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

public class Product {

private boolean[] pro = new boolean[100];
private int head = 0;
private int rear = 0;

public synchronized void production() {
while((rear + 1) % 100 == head) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("生产了一件产品, 放在位置 : " + rear);
pro[rear] = true;
rear = (rear + 1) % 100;
this.notify();
}

public synchronized void consume() {
while(rear == head) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("消费了一件产品, 来自位置 : " + head);
pro[head] = false;
head = (head + 1) % 100;
this.notify();
}
}


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

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

给出解决后的代码:

public class Product {

private boolean[] pro = new boolean[100];
private int head = 0;
private int rear = 0;

public synchronized void production() {
while((rear + 1) % 100 == head) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("生产了一件产品, 放在位置 : " + rear);
pro[rear] = true;
rear = (rear + 1) % 100;
this.notifyAll();
}

public synchronized void consume() {
while(rear == head) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("消费了一件产品, 来自位置 : " + head);
pro[head] = false;
head = (head + 1) % 100;
this.notifyAll();
}
}


import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Product2 {

private boolean[] pro = new boolean[100];
private int head = 0;
private int rear = 0;
Lock lock = new ReentrantLock();
Condition production_con = lock.newCondition();
Condition consume_con = lock.newCondition();

public void production() {
lock.lock();
while((rear + 1) % 100 == head) {
try {
production_con.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
System.out.println("生产了一件产品, 放在位置 : " + rear);
pro[rear] = true;
rear = (rear + 1) % 100;
consume_con.signalAll();
} finally {
lock.unlock();
}
}

public void consume() {
lock.lock();
while(rear == head) {
try {
consume_con.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
System.out.println("消费了一件产品, 来自位置 : " + head);
pro[head] = false;
head = (head + 1) % 100;
production_con.signalAll();
} finally {
lock.unlock();
}
}
}


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

public class Producer implements Runnable {
private Product pro;
public Producer(Product pro) {
this.pro = pro;
}
public void run() {
for(int i = 0; i < 100; i++) {
pro.production();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}


public class Consumer implements Runnable {
private Product pro;
public Consumer(Product pro) {
this.pro = pro;
}
public void run() {
for(int i = 0; i < 100; i++) {
pro.consume();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}


public class Test {

public static void main(String[] args) {
//为了方便阅读,没有用匿名类
Product pro = new Product();
Producer producer = new Producer(pro);
Consumer consumer = new Consumer(pro);
Thread p1 = new Thread(producer);
Thread p2 = new Thread(producer);
Thread c1 = new Thread(consumer);
Thread c2 = new Thread(consumer);
p1.start();
p2.start();
c1.start();
c2.start();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐