Пример #1
0
        /// <summary>
        /// Removes the current thread's value for this thread-local
        /// variable.  If this thread-local variable is subsequently
        /// <seealso cref="#get read"/> by the current thread, its value will be
        /// reinitialized by invoking its <seealso cref="#initialValue"/> method,
        /// unless its value is <seealso cref="#set set"/> by the current thread
        /// in the interim.  This may result in multiple invocations of the
        /// {@code initialValue} method in the current thread.
        ///
        /// @since 1.5
        /// </summary>
        public virtual void Remove()
        {
            ThreadLocalMap m = GetMap(Thread.CurrentThread);

            if (m != null)
            {
                m.Remove(this);
            }
        }
Пример #2
0
        /// <summary>
        /// Sets the current thread's copy of this thread-local variable
        /// to the specified value.  Most subclasses will have no need to
        /// override this method, relying solely on the <seealso cref="#initialValue"/>
        /// method to set the values of thread-locals.
        /// </summary>
        /// <param name="value"> the value to be stored in the current thread's copy of
        ///        this thread-local. </param>
        public virtual void Set(T value)
        {
            Thread         t   = Thread.CurrentThread;
            ThreadLocalMap map = GetMap(t);

            if (map != null)
            {
                map.Set(this, value);
            }
            else
            {
                CreateMap(t, value);
            }
        }
Пример #3
0
        /// <summary>
        /// Variant of set() to establish initialValue. Used instead
        /// of set() in case user has overridden the set() method.
        /// </summary>
        /// <returns> the initial value </returns>
        private T SetInitialValue()
        {
            T              value = InitialValue();
            Thread         t     = Thread.CurrentThread;
            ThreadLocalMap map   = GetMap(t);

            if (map != null)
            {
                map.Set(this, value);
            }
            else
            {
                CreateMap(t, value);
            }
            return(value);
        }
Пример #4
0
        /// <summary>
        /// Returns the value in the current thread's copy of this
        /// thread-local variable.  If the variable has no value for the
        /// current thread, it is first initialized to the value returned
        /// by an invocation of the <seealso cref="#initialValue"/> method.
        /// </summary>
        /// <returns> the current thread's value of this thread-local </returns>
        public virtual T Get()
        {
            Thread         t   = Thread.CurrentThread;
            ThreadLocalMap map = GetMap(t);

            if (map != null)
            {
                ThreadLocalMap.Entry e = map.GetEntry(this);
                if (e != null)
                {
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @SuppressWarnings("unchecked") T result = (T)e.value;
                    T result = (T)e.Value;
                    return(result);
                }
            }
            return(SetInitialValue());
        }
Пример #5
0
 /// <summary>
 /// Factory method to create map of inherited thread locals.
 /// Designed to be called only from Thread constructor.
 /// </summary>
 /// <param name="parentMap"> the map associated with parent thread </param>
 /// <returns> a map containing the parent's inheritable bindings </returns>
 internal static ThreadLocalMap CreateInheritedMap(ThreadLocalMap parentMap)
 {
     return(new ThreadLocalMap(parentMap));
 }