【Java】ThreadSafeStack

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.stream.Stream;

import com.xkind.collections.util.ReadWriteLockExtend;

@SuppressWarnings("serial")
public class ThreadSafeStack<E> extends Stack<E> {

    private final List<E> m_stack;
    
    private static final ReadWriteLock m_signal = new ReentrantReadWriteLock();

    public ThreadSafeStack() {
        m_stack = new LinkedList<E>();
    }

    public ThreadSafeStack(Collection<E> c) {
        m_stack = new LinkedList<E>(c);
    }

    @Override
    public Stream<E> stream() {
        return ReadWriteLockExtend.performReadLock(m_signal, () -> m_stack.stream());
    }

    @Override
    public Stream<E> parallelStream() {
        return ReadWriteLockExtend.performReadLock(m_signal, () -> m_stack.parallelStream());
    }

    @Override
    public E push(E item) {
        return ReadWriteLockExtend.performWriteLock(m_signal, () -> { revert().addFirst(item); return item; });
    }

    @Override
    public E pop() {
        return ReadWriteLockExtend.performWriteLock(m_signal, () -> revert().pollFirst());
    }

    @Override
    public E peek() {
        return ReadWriteLockExtend.performReadLock(m_signal, () -> revert().peekFirst());
    }

    @Override
    public boolean empty() {
        return ReadWriteLockExtend.performReadLock(m_signal, () -> m_stack.isEmpty());
    }

    @Override
    public int search(Object o) {
        return ReadWriteLockExtend.performReadLock(m_signal, () -> m_stack.indexOf(o));
    }

    @Override
    public Object[] toArray() {
        return ReadWriteLockExtend.performReadLock(m_signal, () -> m_stack.toArray());
    }

    @Override
    public <T> T[] toArray(T[] a) {
        return ReadWriteLockExtend.performReadLock(m_signal, () -> m_stack.toArray(a));
    }

    // --------------------------------------------------------------------------------------------

    private LinkedList<E> revert() {
        if (m_stack instanceof LinkedList) {
            return (LinkedList<E>) m_stack;
        } else {
            return new LinkedList<E>(m_stack);
        }
    }

}

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:56  XKIND  阅读(55)  评论(0编辑  收藏  举报