zoukankan      html  css  js  c++  java
  • 【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();
            }
        }
    
    }
    
  • 相关阅读:
    技术博客-3 nginx+uwsgi部署DRF项目踩坑
    Scrum meeting 3
    经典模型及简单模型实践
    inference样例(一)
    Scrum meeting 2
    技术博客-2 DRF用户权限以及邮箱验证
    网页加载慢,你知道几种原因?
    服务器无法访问,如何迅速精准排查定位
    什么是单点登录?单点登录的三种实现方式
    linux中 替换内容的命令
  • 原文地址:https://www.cnblogs.com/zhuzhongxing/p/14147108.html
Copyright © 2011-2022 走看看