zoukankan      html  css  js  c++  java
  • 【Java】ThreadSafeQueue

    import java.util.Collection;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Spliterator;
    import java.util.concurrent.locks.ReadWriteLock;
    import java.util.concurrent.locks.ReentrantReadWriteLock;
    import java.util.function.Consumer;
    import java.util.function.Predicate;
    import java.util.stream.Stream;
    
    import com.xkind.collections.util.ReadWriteLockExtend;
    
    public class ThreadSafeQueue<E> implements Queue<E> {
    
        private final Queue<E> m_queue;
        
        private static final ReadWriteLock m_signal = new ReentrantReadWriteLock();
    
        public ThreadSafeQueue() {
            m_queue = new LinkedList<E>();
        }
        
        public ThreadSafeQueue(Collection<E> c) {
            m_queue = new LinkedList<E>(c);
        }
    
        @Override
        public int size() {
            return ReadWriteLockExtend.performReadLock(m_signal, () -> m_queue.size());
        }
    
        @Override
        public boolean isEmpty() {
            return ReadWriteLockExtend.performReadLock(m_signal, () -> m_queue.isEmpty());
        }
    
        @Override
        public boolean contains(Object o) {
            return ReadWriteLockExtend.performReadLock(m_signal, () -> m_queue.contains(o));
        }
    
        @Override
        public Iterator<E> iterator() {
            return ReadWriteLockExtend.performReadLock(m_signal, () -> revert().iterator());
        }
    
        @Override
        public Object[] toArray() {
            return ReadWriteLockExtend.performReadLock(m_signal, () -> m_queue.toArray());
        }
    
        @Override
        public <T> T[] toArray(T[] a) {
            return ReadWriteLockExtend.performReadLock(m_signal, () -> m_queue.toArray(a));
        }
    
        @Override
        public boolean remove(Object o) {
            return ReadWriteLockExtend.performWriteLock(m_signal, () -> m_queue.remove(o));
        }
    
        @Override
        public boolean containsAll(Collection<?> c) {
            return ReadWriteLockExtend.performReadLock(m_signal, () -> m_queue.containsAll(c));
        }
    
        @Override
        public boolean addAll(Collection<? extends E> c) {
            return ReadWriteLockExtend.performWriteLock(m_signal, () -> m_queue.addAll(c));
        }
    
        @Override
        public boolean removeAll(Collection<?> c) {
            return ReadWriteLockExtend.performWriteLock(m_signal, () -> m_queue.removeAll(c));
        }
    
        @Override
        public boolean retainAll(Collection<?> c) {
            return ReadWriteLockExtend.performReadLock(m_signal, () -> m_queue.retainAll(c));
        }
    
        @Override
        public void clear() {
            ReadWriteLockExtend.performWriteLock(m_signal, () -> m_queue.clear());
        }
    
        @Override
        public boolean add(E e) {
            return ReadWriteLockExtend.performWriteLock(m_signal, () -> m_queue.add(e));
        }
    
        @Override
        public boolean offer(E e) {
            return ReadWriteLockExtend.performWriteLock(m_signal, () -> m_queue.offer(e));
        }
    
        @Override
        public E remove() {
            return ReadWriteLockExtend.performWriteLock(m_signal, () -> m_queue.remove());
        }
    
        @Override
        public E poll() {
            return ReadWriteLockExtend.performWriteLock(m_signal, () -> m_queue.poll());
        }
    
        @Override
        public E element() {
            return ReadWriteLockExtend.performReadLock(m_signal, () -> m_queue.element());
        }
    
        @Override
        public E peek() {
            return ReadWriteLockExtend.performReadLock(m_signal, () -> m_queue.peek());
        }
    
        @Override
        public boolean removeIf(Predicate<? super E> filter) {
            return ReadWriteLockExtend.performWriteLock(m_signal, () -> m_queue.removeIf(filter));
        }
    
        @Override
        public Spliterator<E> spliterator() {
            return ReadWriteLockExtend.performReadLock(m_signal, () -> m_queue.spliterator());
        }
    
        @Override
        public Stream<E> stream() {
            return ReadWriteLockExtend.performReadLock(m_signal, () -> m_queue.stream());
        }
    
        @Override
        public Stream<E> parallelStream() {
            return ReadWriteLockExtend.performReadLock(m_signal, () -> m_queue.parallelStream());
        }
    
        @Override
        public void forEach(Consumer<? super E> action) {
            ReadWriteLockExtend.performReadLock(m_signal, () -> m_queue.forEach(action));
        }
    
        // ----------------------------------------------------------------------------------
    
        private LinkedList<E> revert() {
            if (m_queue instanceof LinkedList) {
                return (LinkedList<E>) m_queue;
            } else {
                return new LinkedList<E>(m_queue);
            }
        }
    
    }
    

    ReadWriteLockExtend

    package com.xkind.collections.util;
    
    import java.util.concurrent.locks.ReadWriteLock;
    import java.util.function.Supplier;
    
    public class ReadWriteLockExtend {
    
        public static void performReadLock(final ReadWriteLock signal, final Runnable runnable) {
            signal.readLock().lock();
            try {
                runnable.run();
            } finally {
                signal.readLock().unlock();
            }
        }
    
        public static <T> T performReadLock(final ReadWriteLock signal, final Supplier<T> supplier) {
            signal.readLock().lock();
            try {
                return supplier.get();
            } finally {
                signal.readLock().unlock();
            }
        }
    
        public static void performWriteLock(final ReadWriteLock signal, final Runnable runnable) {
            signal.writeLock().lock();
            try {
                runnable.run();
            } finally {
                signal.writeLock().unlock();
            }
        }
    
        public static <T> T performWriteLock(final ReadWriteLock signal, final Supplier<T> supplier) {
            signal.writeLock().lock();
            try {
                return supplier.get();
            } finally {
                signal.writeLock().unlock();
            }
        }
    
    }
    
  • 相关阅读:
    java实现定时任务(Quartz)
    java实现发送邮件工具
    mysql服务器查询慢原因分析方法
    Vue 插槽
    Vue组件参数传递问题
    Vue路由
    Spring Boot2 拦截器对静态资源的放行
    Spring Boot过滤非法请求
    U盘安装linux(CentOS Kali ubuntu)无法挂载_实测
    如何编译Java程序以及运行程序
  • 原文地址:https://www.cnblogs.com/zhuzhongxing/p/14147109.html
Copyright © 2011-2022 走看看