java并发-生产者与消费者大揭秘

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_22271479/article/details/85109380

生产者与消费者大揭秘

notify/signal和notifyAll/signalAll的区别

前者是从等待队列中挑一个进入获取锁的队列去竞争锁
后者是将所有的等待队列全部移到获取锁队列中,都属于竞争状态

notify/signal的问题

当出现wait条件不相同的线程在竞争所资源,那么这种范式很大几率会造成 假死锁 的状态
说明白点就是,比如当绝大多数线程都处于wait状态,条件处于临界值, a>0 wait的线程唤醒了同类线程,那么还是会处于wait状态,那么wait/notify机制将失效。

Lock的优势

在Lock的Condition协调器,可以不同的状态附属一个Condition,那么两种状态的线程,将属于两个等待队列,同一个队列中的线程是不可能被唤醒的,so,就不存在synchronized
这种方式造成的问题

接口

操作中心

/**
 * describe:
 * E-mail:[email protected]  date:2018/12/19
 *
 * @Since 0.0.1
 */
public interface ICenter {
    void produce();

    void consume();
}

生产者

/**
 * describe:
 * E-mail:[email protected]  date:2018/12/19
 *
 * @Since 0.0.1
 */
public interface IProducer<T> {
    int MIN_NUM = 0;

    void produce(T t);
}

消费者

/**
 * describe:
 * E-mail:[email protected]  date:2018/12/19
 *
 * @Since 0.0.1
 */
public interface IConsumer<T> {
    void consume(T t);
}

通过synchronized

生产者

/**
 * describe:
 * E-mail:[email protected]  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;
                t.add(p);
                System.out.println(p + " 当前数量" + t.size());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                t.notify();
            }
        }
    }
}

消费者

扫描二维码关注公众号,回复: 4609920 查看本文章
/**
 * describe:
 * E-mail:[email protected]  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();
                t.notify();
                System.out.println("消费:" + produce + " 当前数量" + t.size());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

通过Lock

生产者

/**
 * describe:
 * E-mail:[email protected]  date:2018/12/19
 *
 * @Since 0.0.1
 */
/**
 * describe:
 * E-mail:[email protected]  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();
            } catch (Exception e) {
                t.notifyAll();
            }
        }
    }
}

消费者

/**
 * describe:
 * E-mail:[email protected]  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());
                t.notifyAll();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }catch (Exception e){
                e.printStackTrace();
                t.notifyAll();
            }
        }
    }
}

测试类

/**
 * describe:
 * E-mail:[email protected]  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

GitHub主页

猜你喜欢

转载自blog.csdn.net/qq_22271479/article/details/85109380