多线程-线程通信的三种(jvm,jdk,Guava)

import com.google.common.util.concurrent.Monitor;

import java.util.LinkedList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;

/**
 *
 */
public class MonitorExample {

    /**
     * Guava
     */
    static class MonitorGuard {
        private final LinkedList quene = new LinkedList<>();
        private final int MAX = 10;
        private final Monitor monitor = new Monitor();

        private final Monitor.Guard CAN_OFFER = monitor.newGuard(() -> quene.size() < MAX);
        private final Monitor.Guard CAN_TAKE = monitor.newGuard(() -> quene.isEmpty());

        public void offer(int value) {
            try {
                monitor.enterWhen(CAN_OFFER);
                quene.addLast(value);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                monitor.leave();
            }
        }

        public int take() {
            try {
                monitor.enterWhen(CAN_TAKE);
                return quene.removeFirst();
            } catch (InterruptedException e) {
                throw new RuntimeException(".......");
            } finally {
                monitor.leave();
            }
        }
    }

    /**
     * jdk
     */
    static class LockCondition {
        private final ReentrantLock lock = new ReentrantLock();
        private final Condition FULL_CONDITION = lock.newCondition();
        private final Condition EMPTY_CONDITION = lock.newCondition();
        private final LinkedList quene = new LinkedList<>();
        private final int MAX = 10;

        public void offer(int value) {
            try {
                lock.lock();
                while (quene.size() > MAX) {
                    FULL_CONDITION.await();
                }
                quene.addLast(value);
                EMPTY_CONDITION.signalAll();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }

        public int take() {
            Integer value = null;
            try {
                lock.lock();
                while (quene.isEmpty()) {
                    EMPTY_CONDITION.await();
                }
                value = quene.removeFirst();
                FULL_CONDITION.signal();

            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
            return value;
        }


        private  void doAction(Consumer consumer, T t) {
            try {
                lock.lock();
                consumer.accept(t);
            } finally {
                lock.unlock();
            }
        }
    }

    /**
     * jvm
     */
    static class Synchronized {
        private final LinkedList quene = new LinkedList<>();
        private final int MAX = 10;

        public void offer(int value) {
            synchronized (quene) {
                while (quene.size() > MAX) {
                    try {
                        quene.wait();  //一定是当前对象的wait,以及当前对象的syn,并且释放锁
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                quene.addLast(value);
                quene.notifyAll();
            }
        }


        public Integer take() {
            synchronized (quene) {
                while (quene.isEmpty()) {
                    try {
                        quene.wait();  //一定是当前对象的wait,以及当前对象的syn,并且释放锁
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                Integer value = quene.removeFirst();
                quene.notifyAll();
                return value;
            }
        }

    }


    public static void main(String[] args) {
        final Synchronized s = new Synchronized();
        final AtomicInteger counter = new AtomicInteger();
        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                for (; ; )
                    try {
                        int data = counter.getAndIncrement();
                        System.out.println(Thread.currentThread().getName() + "------offer-----" + data);
                        s.offer(data);
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
            }).start();
        }
        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                for (; ; )
                    try {
                        Integer data = s.take();
                        System.out.println(Thread.currentThread().getName() + "------take------" + data);
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
            }).start();
        }
    }
}

你可能感兴趣的:(多线程-线程通信的三种(jvm,jdk,Guava))