文章目录
生产者与消费者大揭秘
notify/signal和notifyAll/signalAll的区别
前者是从等待队列中挑一个进入获取锁的队列去竞争锁
后者是将所有的等待队列全部移到获取锁队列中,都属于竞争状态
notify/signal的问题
当出现wait条件不相同的线程在竞争所资源,那么这种范式很大几率会造成 假死锁 的状态
说明白点就是,比如当绝大多数线程都处于wait状态,条件处于临界值, a>0 wait的线程唤醒了同类线程,那么还是会处于wait状态,那么wait/notify机制将失效。
Lock的优势(Condition详解)
再Lock的Condition协调器,可以不同的状态附属一个Condition,那么两种状态的线程,将属于两个等待队列,同一个队列中的线程是不可能被唤醒的,so,就不存在synchronized
这种方式造成的问题
接口
操作中心
/**
* describe:
* E-mail:yzzstyle@163.com date:2018/12/19
*
* @Since 0.0.1
*/
public interface ICenter {
void produce();
void consume();
}
生产者
/**
* describe:
* E-mail:yzzstyle@163.com date:2018/12/19
*
* @Since 0.0.1
*/
public interface IProducer<T> {
int MIN_NUM = 0;
void produce(T t);
}
消费者
/**
* describe:
* E-mail:yzzstyle@163.com date:2018/12/19
*
* @Since 0.0.1
*/
public interface IConsumer<T> {
void consume(T t);
}
通过synchronized
生产者
/**
* describe:
* E-mail:yzzstyle@163.com date:2018/12/19
*
* @Since 0.0.1
*/
public class SyncProducer<T extends Deque> implements IProducer<T> {
@Override
public void produce(T t) {
synchronized (t) {
try {
if (t.size() > MIN_NUM) {
t.wait();
}
if (t.size() > MIN_NUM) return;
//生产
String p = "产品:" + t.size() + 1;
final boolean add = t.add(p);
System.out.println(p + " 当前数量" + t.size() + add);
t.notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
t.notifyAll();
}
}
}
}
消费者
/**
* describe:
* E-mail:yzzstyle@163.com date:2018/12/19
*
* @Since 0.0.1
*/
public class SynConsumer<T extends Deque> implements IConsumer<T> {
@Override
public void consume(T t) {
synchronized (t) {
try {
if (t.size() == 0) {
t.wait();
}
if (t.size() == 0) return;
//消费
Object produce = t.pollFirst();
System.out.println("消费:" + produce + " 当前数量" + t.size());
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
t.notifyAll();
}
}
}
}
通过Lock
生产者
/**
* describe:
* E-mail:yzzstyle@163.com date:2018/12/19
*
* @Since 0.0.1
*/
public class SingleProducer<T extends Deque> implements IProducer<T> {
private Lock lock;
private Condition conditionForTake;
private Condition conditionForPut;
public SingleProducer(Lock lock, Condition conditionForTake, Condition conditionForPut) {
this.lock = lock;
this.conditionForTake = conditionForTake;
this.conditionForPut = conditionForPut;
}
@Override
public void produce(T t) {
lock.lock();
try {
if (t.size() > MIN_NUM) {
conditionForPut.await();
}
if (t.size() > MIN_NUM) return;
//生产
String p = "产品:" + t.size() + 1;
t.add(p);
conditionForTake.signal();
System.out.println(p + " 当前数量" + t.size());
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
消费者
/**
* describe:
* E-mail:yzzstyle@163.com date:2018/12/19
*
* @Since 0.0.1
*/
public class SingleConsumer<T extends Deque> implements IConsumer<T> {
private Lock lock;
private Condition conditionForTake;
private Condition conditionForPut;
public SingleConsumer(Lock lock, Condition conditionForTake, Condition conditionForPut) {
this.lock = lock;
this.conditionForTake = conditionForTake;
this.conditionForPut = conditionForPut;
}
@Override
public void consume(T t) {
lock.lock();
try {
//if (t.size() == 0){
while (t.size() == 0) {
conditionForTake.await();
}
//if (t.size() == 0) return;
Object produce = t.pollFirst();
System.out.println("消费:" + produce + " 当前数量" + t.size());
conditionForPut.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
测试类
/**
* describe:
* E-mail:yzzstyle@163.com date:2018/12/19
*
* @Since 0.0.1
*/
public class Test {
public static void run(ICenter center) {
for (int i = 0; i < 20; i++) {
Thread thread = new Thread(() -> {
while (true) {
center.produce();
}
});
thread.start();
}
for (int i = 0; i < 30; i++) {
Thread thread = new Thread(() -> {
while (true) {
center.consume();
}
});
thread.start();
}
}
public static void syncPc() {
LinkedList<String> linkedList = new LinkedList<>();
IProducer<LinkedList<String>> producer = new SyncProducer<>();
IConsumer<LinkedList<String>> consumer = new SynConsumer<>();
ICenter center = new Center<>(linkedList, producer, consumer);
run(center);
}
public static void syncSingle() {
//公平锁
Lock lock = new ReentrantLock(false);
//两个协调者,一个负责生产,一个负责消费
Condition conditionForTake = lock.newCondition();
Condition conditionForPut = lock.newCondition();
LinkedList<String> linkedList = new LinkedList<>();
IProducer<LinkedList<String>> producer = new SingleProducer<>(lock, conditionForTake, conditionForPut);
IConsumer<LinkedList<String>> consumer = new SingleConsumer<>(lock, conditionForTake, conditionForPut);
ICenter center = new Center<>(linkedList, producer, consumer);
run(center);
}
public static void main(String[] args) {
syncSingle();
}
}
其他方式
- 通过CAS无锁机制来更新共享资源,解决线程安全问题(Unsafe这个类)
- 通过阻塞队列来做(BlockingQueue的实现类来做)
- 通过无阻塞队列来做ConcurrentLinkedQueue