Class DisposableThreadLocal<T>
.NET's built-in System.Threading.ThreadLocal`1 has a serious flaw: internally, it creates an array with an internal lattice structure which in turn causes the garbage collector to cause long blocking pauses when tearing the structure down. See https://ayende.com/blog/189761-A/production-postmortem-the-slow-slowdown-of-large-systems for a more detailed explanation.
This is a completely different problem than in Java which the ClosableThreadLocal<T> class is meant to solve, so DisposableThreadLocal<T> is specific to Lucene.NET and can be used as a direct replacement for ClosableThreadLocal<T>.
This class works around the issue by using an alternative approach than using System.Threading.ThreadLocal`1. It keeps track of each thread's local and global state in order to later optimize garbage collection. A complete explanation can be found at https://ayende.com/blog/189793-A/the-design-and-implementation-of-a-better-threadlocal-t.
Note
This API is for internal purposes only and might change in incompatible ways in the next release.
Inheritance
Implements
Inherited Members
Namespace: Lucene.Net.Util
Assembly: Lucene.Net.dll
Syntax
public sealed class DisposableThreadLocal<T> : IDisposable
Type Parameters
Name | Description |
---|---|
T | Specifies the type of data stored per-thread. |
Constructors
| Improve this Doc View SourceDisposableThreadLocal()
Initializes the DisposableThreadLocal<T> instance.
Declaration
public DisposableThreadLocal()
Remarks
The default value of T
is used to initialize
the instance when Value is accessed for the first time.
DisposableThreadLocal(Func<T>)
Initializes the DisposableThreadLocal<T> instance with the
specified valueFactory
function.
Declaration
public DisposableThreadLocal(Func<T> valueFactory)
Parameters
Type | Name | Description |
---|---|---|
System.Func<T> | valueFactory | The System.Func<T, TResult> invoked to produce a lazily-initialized value when an attempt is made to retrieve Value without it having been previously initialized. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
|
Properties
| Improve this Doc View SourceIsValueCreated
Gets whether Value is initialized on the current thread.
Declaration
public bool IsValueCreated { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
Exceptions
Type | Condition |
---|---|
System.ObjectDisposedException | The DisposableThreadLocal<T> instance has been disposed. |
Value
Gets or sets the value of this instance for the current thread.
Declaration
public T Value { get; set; }
Property Value
Type | Description |
---|---|
T |
Remarks
If this instance was not previously initialized for the current thread, accessing Value will attempt to
initialize it. If an initialization function was supplied during the construction, that initialization
will happen by invoking the function to retrieve the initial value for Value. Otherwise, the default
value of T
will be used.
Exceptions
Type | Condition |
---|---|
System.ObjectDisposedException | The DisposableThreadLocal<T> instance has been disposed. |
Values
Gets a collection for all of the values currently stored by all of the threads that have accessed this instance.
Declaration
public ICollection<T> Values { get; }
Property Value
Type | Description |
---|---|
System.Collections.Generic.ICollection<T> |
Exceptions
Type | Condition |
---|---|
System.ObjectDisposedException | The DisposableThreadLocal<T> instance has been disposed. |
Methods
| Improve this Doc View SourceDispose()
Releases the resources used by this DisposableThreadLocal<T> instance.
Declaration
public void Dispose()
Get()
Declaration
[Obsolete("Use Value instead. This method will be removed in 4.8.0 release candidate.")]
public T Get()
Returns
Type | Description |
---|---|
T |
Set(T)
Declaration
[Obsolete("Use Value instead. This method will be removed in 4.8.0 release candidate.")]
public void Set(T value)
Parameters
Type | Name | Description |
---|---|---|
T | value |