Java4Android程序员

Java多线程之并发协作生产者消费者设计模式JDK1.5.0+升

2016-10-16  本文已影响65人  亮之于东

上一篇[Java多线程之并发协作生产者消费者设计模式]已经讲述了在Java的多线程中,如何处理并发安全的生产者消费者设计模式,不了解的可以先看看上一篇的内容

上一篇中的效率和优化问题

jdk1.5的对多线程并发安全的新解决方案

最终经过优化的代码



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

/**
 * Created by yuandl on 2016-10-12.
 * 要生产的资源
 */
public class Resource {
    private int number = 0;//资源编号
    private boolean flag = false;//资源标记  时候有资源

    /* 创建一个锁对象。*/
    private Lock lock = new ReentrantLock();//锁
    /*通过已有的锁获取该锁上的监视器对象。通过已有的锁获取两组监视器,一组监视生产者,一组监视消费者。*/
    private Condition condition_consumer = lock.newCondition();//消费者对锁的监视器
    private Condition condition_producer = lock.newCondition();//生产者对锁的监视器

    public void create() {
        lock.lock();
        try {
            while (flag) {
                condition_producer.await();
            }
            number++;
            System.out.println("生产者--------------" + number);
            flag = true;
            condition_consumer.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void destroy() {
        lock.lock();
        try {
            while (!flag) {
                condition_consumer.await();
            }
            System.out.println("消费者***" + number);
            flag = false;
            condition_producer.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}



/**
 * Created by yuandl on 2016-10-12.
 * 生产者
 */
public class Producer implements Runnable {
    private Resource resource;

    public Producer(Resource resource) {
        this.resource = resource;
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            resource.create();
        }
    }
}



/**
 * Created by yuandl on 2016-10-12.
 * 消费者
 */
public class Consumer implements Runnable {
    private Resource resource;

    public Consumer(Resource resource) {
        this.resource = resource;
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            resource.destroy();
        }
    }
}

package thread.produceconsumer2;

/**
 * Created by yuandl on 2016-10-12.
 */
public class MainTest {
    public static void main(String args[]) {
        Resource resource = new Resource();
        new Thread(new Producer(resource)).start();
        new Thread(new Producer(resource)).start();
        new Thread(new Consumer(resource)).start();
        new Thread(new Consumer(resource)).start();
    }
}



生产者--------------381
消费者***381
生产者--------------382
消费者***382
生产者--------------383
消费者***383
生产者--------------384
消费者***384
生产者--------------385
消费者***385
生产者--------------386
消费者***386
生产者--------------387
消费者***387
生产者--------------388
消费者***388
生产者--------------389
消费者***389
生产者--------------390
消费者***390
生产者--------------391
消费者***391
上一篇下一篇

猜你喜欢

热点阅读