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();
            }
        }
    
    }
    
  • 相关阅读:
    C#Socket发16进制以及进制转换
    WPF跨线程操作UI界面控件
    DispatcherTimer和Timer的区别
    C#等比列放大缩小图片
    计算进项税
    AX 中通过SqlServer数据库刷数据
    AX中日期的常用方法
    从AX2012系统批量生成CSV格式数据
    装箱单过发票
    导入CSV文件乱码
  • 原文地址:https://www.cnblogs.com/zhuzhongxing/p/14147108.html
Copyright © 2011-2022 走看看