多线程-线程通信的三种(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<Integer> 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<Integer> 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 <T> void doAction(Consumer<T> consumer, T t) {
            try {
                lock.lock();
                consumer.accept(t);
            } finally {
                lock.unlock();
            }
        }
    }

    /**
     * jvm
     */
    static class Synchronized {
        private final LinkedList<Integer> 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();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值