【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();
        }
    }

}
posted @ 2019-03-13 14:55  XKIND  阅读(103)  评论(0编辑  收藏  举报