码迷,mamicode.com
首页 > 编程语言 > 详细

Java-JUC(九):使用Lock替换synchronized,使用Condition的await,singal,singalall替换object的wait,notify,notifyall实现线程间的通信

时间:2018-04-29 23:55:02      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:system   变量   pre   有关   star   --   color   condition   read   

Condition:

  • condition接口描述了可能会与锁有关的条件变量。这些用法上与使用object.wait访问隐式监视器类似,但提供了更强大的功能。需要特别指出的是,单个lock可能与多个Condition对象关联。为了避免兼容性问题,Condition方法的名称与对应的objec版本中的不同。
  • 在condition对象中,与wait、notify、notifyall方法对应的分别是await、singal、signalAll
  • Condition实例实质上被绑定到一个锁上。要为特定Lock实例获得Condition实例,请使用其newCondition()方法。

使用Lock替换Synchronized,使用Condition替换Ojbect实现线程间的通信

例子如下:

package com.dx.juc.test;

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

public class LockAndConditionTest {
    public static void main(String[] args) {
        Clerk clerk = new Clerk();
        Productor productor = new Productor(clerk);
        Consumer consumer = new Consumer(clerk);

        new Thread(productor, "Productor-A").start();
        new Thread(consumer, "Consumer-A").start();

        new Thread(productor, "Productor-B").start();
        new Thread(consumer, "Consumer-B").start();
    }
}

/**
 * 店员
 */
class Clerk {
    private int product = 0;
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    /**
     * 进货
     */
    public void purchase() {
        lock.lock();

        try {
            while (product >= 1) {
                System.out.println(Thread.currentThread().getName() + ":" + "产品已满。。。");
                try {
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            System.out.println(Thread.currentThread().getName() + ":" + ++product);
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }

    /**
     * 卖货
     */
    public void sell() {
        lock.lock();

        try {
            while (product <= 0) {
                System.out.println(Thread.currentThread().getName() + ":" + "产品缺貨。。。");
                try {
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            System.out.println(Thread.currentThread().getName() + ":" + --product);
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }
}

/**
 * 生产者 不断的生产产品给店员
 */
class Productor implements Runnable {
    private Clerk clerk;

    public Productor(Clerk clerk) {
        this.clerk = clerk;
    }

    public void run() {
        for (int i = 0; i < 2; i++) {
            clerk.purchase();
        }
    }
}

/**
 * 消费者 不断的从店员那里消费产品
 */
class Consumer implements Runnable {
    private Clerk clerk;

    public Consumer(Clerk clerk) {
        this.clerk = clerk;
    }

    public void run() {
        for (int i = 0; i < 2; i++) {
            clerk.sell();
        }
    }
}

 

Java-JUC(九):使用Lock替换synchronized,使用Condition的await,singal,singalall替换object的wait,notify,notifyall实现线程间的通信

标签:system   变量   pre   有关   star   --   color   condition   read   

原文地址:https://www.cnblogs.com/yy3b2007com/p/8972263.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!