前言
这篇文章,主要是说用法,其实这个使用还是很多的,根据一段生产消费者程序,来说一说。
一个栗子
package com.pochi.juc;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class ReentrenLockProblem {
public static void main(String[] args) {
// 弄个产品出来
Product product = new Product();
// 生产者消费者也要new一下
Producer producer = new Producer(product);
Consumer consumer = new Consumer(product);
// 整了四条线程,俩生产,俩消费
Thread thread = new Thread(producer);
Thread thread1 = new Thread(consumer);
Thread thread2 = new Thread(producer);
Thread thread3 = new Thread(consumer);
//开始!
thread.start();
thread1.start();
thread2.start();
thread3.start();
}
}
// 生产者
class Producer implements Runnable{
// 拿到产品
private Product product;
public Producer(Product product) {
this.product = product;
}
@Override
public void run() {
// 我不管,我穷我就要一直生产,我的加班费!!!
while (true){
product.produce();
}
}
}
// 消费者
class Consumer implements Runnable{
// 肯定要拿到产品
private Product product;
public Consumer(Product product) {
this.product = product;
}
@Override
public void run() {
// 我不管,我有钱任性,你有多少我买多少。
while (true){
product.sale();
}
}
}
// 产品类
class Product{
// 产品名都没了,就个数量
private int count;
// 为了保证产品被多个线程访问的时候同步,要锁,显式的锁
private ReentrantLock lock=new ReentrantLock();
// 为了保证产品不要没库存了还卖,超库存了还生产,搞俩监视器,盯着
private Condition notFull=lock.newCondition();
private Condition notEmpty=lock.newCondition();
// 生产产品
public void produce(){
// 外人谢绝入内,只有我们生产一线能进来
lock.lock();
// 生产完,别的部门也要进来,要保证锁一定会被打开,必须finally
try {
// 没错,我们的库存就是1
while(count>=1){
// 大于等于库存,报警
System.out.println("Full");
// 这个生产线等着吧,锁也给我放开
notFull.await();
}
// 不到库存的话,那就是打印一下生产完的存库
System.out.println(Thread.currentThread().getName()+" "+(++count));
// 告诉隔壁销售组的工友,这里库存不是空的了,反正至少有我们刚生产的一个
notEmpty.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// 一定要释放锁
lock.unlock();
}
}
// 销售方法
public void sale(){
// 我们销售一组的人卖,其他人别BB
lock.lock();
try {
// 没库存的时候
while(count<=0){
// 发出警报
System.out.println("empty");
// 我们去隔壁磕个瓜子,等生产
notEmpty.await();
}
// 有库存就打印卖完剩下的库存
System.out.println(Thread.currentThread().getName()+" "+(--count));
// 告诉生产,至少现在不会是满库存了,可以动起来了
notFull.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// 释放锁不能忘
lock.unlock();
}
}
}
结果
(节选)
Full
Thread-1 0
empty
Thread-2 1
Full
Thread-3 0
empty
Thread-0 1
Full
Thread-1 0
empty
后记
就这样吧~