zoukankan      html  css  js  c++  java
  • 弱引用对象池

    第一:定义一个实现栈的接口:

        /// <summary>
        /// 栈接口,栈抽象数据类型
        /// </summary>
        public interface IStack<T>
        {
            /// <summary>
            /// 是否为空栈
            /// </summary>
            /// <returns></returns>
            bool IsEmpty();
            /// <summary>
            ///  取出栈顶元素,未出栈
            /// </summary>
            T Peek();
            /// <summary>
            /// 出栈
            /// </summary>
            /// <returns></returns>
            T Pop();
            /// <summary>
            /// 入栈
            /// </summary>
            /// <param name="item">元素</param>
            /// <returns></returns>
            void Push(T item);
        }

    第二:链式栈数据结构:

        /// <summary>
        /// 构建链栈指针
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public class Node<T>
        {
            /// <summary>
            /// 数据域  保存数据元素
            /// </summary>
            public T Data;
            /// <summary>
            /// 后继结点指针
            /// </summary>
            public Node<T> Next;
            public Node(T data, Node<T> next) {
                this.Data = data;
                this.Next = next;
            }
        }

        /// <summary>
        /// 自定义数据结构 链式栈
        /// </summary>
        /// <typeparam name="T">T</typeparam>
        public class LinkedStack<T> : IStack<T>
        {
            private int size;
            private object syncRoot;
            /// <summary>
            /// 节点指针
            /// </summary>
            private Node<T> top;
            /// <summary>
            /// 构造一个空的链栈
            /// </summary>
            public LinkedStack()
            {
                this.top = null;
            }
            /// <summary>
            /// 判断一个链栈是否为空 True 为空
            /// </summary>
            /// <returns></returns>
            public bool IsEmpty()
            {
                return this.top == null;
            }
            /// <summary>
            /// 取出栈顶元素 若栈为空返回null
            /// </summary>
            public T Peek()
            {
                return this.top == null ? default(T) : this.top.Data;
            }
            /// <summary>
            /// 出栈 若栈为空返回null
            /// </summary>
            /// <returns></returns>
            public T Pop()
            {
                if (this.top == null)
                    return default(T);
                T temp = this.top.Data;   //取栈顶结点元素
                this.top = this.top.Next; //删除栈顶结点
                size--;
                return temp;
            }
            /// <summary>
            /// 入栈
            /// </summary>
            /// <param name="item">元素</param>
            public void Push(T item)
            {
                if (item != null)
                {
                    this.top = new Node<T>(item, top);
                    size++;
                }
            }
            /// <summary>
            /// 获取链栈元素数
            /// </summary>
            public int Count
            {
                get { return this.size; }
            }
            /// <summary>
            /// 是否是线程安全的
            /// </summary>
            public bool IsSynchronized
            {
                get {
                    return false;
                }
            }
            /// <summary>
            /// 同步访问对象
            /// </summary>
            public object SyncRoot
            {
                get {
                    if (this.syncRoot == null)
                        Interlocked.CompareExchange<object>(ref this.syncRoot, new object(), null);
                    return this.syncRoot;
                }
            }
            /// <summary>
            /// 找出链栈相同元素
            /// </summary>
            /// <param name="value">value</param>
            /// <returns></returns>
            public Node<T> Find(T value)
            {
                Node<T> head = this.top;
                EqualityComparer<T> comparer = EqualityComparer<T>.Default;
                if (head != null)
                {
                    if (value != null)
                    {
                        do
                        {
                            if (head != null)
                            {
                                if (comparer.Equals(head.Data, value))
                                {
                                    return head;
                                }
                                head = head.Next;
                            }
                            else
                            {
                                return null;
                            }
                        }
                        while (head != this.top);
                    }
                    else
                    {
                        do
                        {
                            if (head != null)
                            {
                                if (head.Data == null)
                                {
                                    return head;
                                }
                                head = head.Next;
                            }
                            else {
                                return null;
                            }
                        }
                        while (head != this.top);
                    }
                }
                return null;
            }
            /// <summary>
            /// 对象比较
            /// </summary>
            /// <param name="item">item</param>
            /// <returns></returns>
            public bool Contains(T item)
            {
                return (this.Find(item) != null);
            }
            /// <summary>
            /// 清空链栈
            /// </summary>
            public void Clear()
            {
                Node<T> head = this.top;
                while (head != null)
                {
                    Node<T> node1 = head;
                    head = head.Next;
                    node1.Next = null;
                }
                this.top = null;
                this.size = 0;
            }
        }

    第三:封装弱引用对象池

         /// <summary>
        /// 构造弱引用对象池
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public sealed class WeakObjectPool<T> where T : class
        {
            private WeakReference stackRef;
            /// <summary>
            /// 获取资源数
            /// </summary>
            public int Size
            {
                get
                {
                    return this.LinkedStack.Count;
                }
            }
            /// <summary>
            /// 是否是线程安全的
            /// </summary>
            public bool IsSynchronized
            {
                get { return true; }
            }
            /// <summary>
            /// 获取同步访问对象
            /// </summary>
            public object SyncRoot
            {
                get { return stackRef; }
            }
            private LinkedStack<T> LinkedStack
            {
                get
                {
                    if (stackRef.IsAlive)
                    {
                        return stackRef.Target as LinkedStack<T>;
                    }
                    else
                    {
                        LinkedStack<T> linkedStack = new LinkedStack<T>();
                        lock (SyncRoot)
                        {
                            stackRef.Target = linkedStack;
                        }
                        return linkedStack;
                    }
                }
            }
            public WeakObjectPool()
            {
                stackRef = new WeakReference(new LinkedStack<T>());
            }
            /// <summary>
            /// 将对象放入池中
            /// </summary>
            /// <param name="t">T</param>
            public void Entrance(T t)
            {
                if (t != null)
                {
                    LinkedStack<T> linkedStack = this.LinkedStack;
                    if (!linkedStack.Contains(t))
                    {
                        lock (linkedStack)
                        {
                            if (!linkedStack.Contains(t))
                            {
                                linkedStack.Push(t);
                            }
                        }
                    }
                }
            }
            /// <summary>
            /// 取出池中顶部对象.移除对象
            /// </summary>
            /// <returns>T</returns>
            public T Export()
            {
                LinkedStack<T> stack = this.LinkedStack;
                if (stack.Count > 0)
                {
                    lock (SyncRoot)
                    {
                        if (stack.Count > 0)
                        {
                            return stack.Pop();
                        }
                    }
                }
                return default(T);
            }
            /// <summary>
            /// 取栈顶对象.对象不移除
            /// </summary>
            /// <returns></returns>
            public T Fetch()
            {
                LinkedStack<T> stack = this.LinkedStack;
                if (stack.Count > 0)
                {
                    lock (SyncRoot)
                    {
                        if (stack.Count > 0)
                        {
                            return stack.Peek();
                        }
                    }
                }
                return default(T);
            }
            /// <summary>
            /// 清空资源
            /// </summary>
            public void Clear()
            {
                LinkedStack<T> stack = this.LinkedStack;
                if (stack.Count > 0)
                {
                    lock (SyncRoot)
                    {
                        stack.Clear();
                    }
                }
            }
        }

  • 相关阅读:
    show proceslist时发现大量的sleep,有什么风险吗,该如何处理?
    监控MySQL的性能,应该主要观察那几个监控项?
    MySQL所有的压力都在一个CPU核心上,为什么会产生这种现象,改如何解决?
    大表,某列无索引,先需要查询该列,删除符合条件的记录,大约占40%数据量,请问有何更好的方案吗?
    MySQL DBA运维中那些动作属于危险性操作?
    云环境上自建MySQL,有哪些高可用实现方案?
    RDS上,MySQL实例中某张表数据小于tmp_table_size,但有查询时会报错临时空间满 The table '/data/mysql/zst/tmp/#sql_13975_23' is full. 原因可能是什么?
    MySQL误删除frm文件该怎么办?
    生产环境MySQL死锁如何监控及如何减少死锁发生的概率。
    MongoDB有哪些优秀特性及适合的场景是什么?
  • 原文地址:https://www.cnblogs.com/yuming1983/p/3509962.html
Copyright © 2011-2022 走看看