C# 创建线程时出现NullReferenceException
我在创建一个简单的线程池时看到了这一点。在那里,我遇到了一个优雅且符合我目的的:C# 创建线程时出现NullReferenceException,c#,multithreading,debugging,nullreferenceexception,C#,Multithreading,Debugging,Nullreferenceexception,我在创建一个简单的线程池时看到了这一点。在那里,我遇到了一个优雅且符合我目的的: using System; using System.Collections.Generic; using System.Threading; namespace SimpleThreadPool { public sealed class Pool : IDisposable { public Pool(int size) { this._
using System;
using System.Collections.Generic;
using System.Threading;
namespace SimpleThreadPool
{
public sealed class Pool : IDisposable
{
public Pool(int size)
{
this._workers = new LinkedList<Thread>();
for (var i = 0; i < size; ++i)
{
var worker = new Thread(this.Worker) { Name = string.Concat("Worker ", i) };
worker.Start();
this._workers.AddLast(worker);
}
}
public void Dispose()
{
var waitForThreads = false;
lock (this._tasks)
{
if (!this._disposed)
{
GC.SuppressFinalize(this);
this._disallowAdd = true; // wait for all tasks to finish processing while not allowing any more new tasks
while (this._tasks.Count > 0)
{
Monitor.Wait(this._tasks);
}
this._disposed = true;
Monitor.PulseAll(this._tasks); // wake all workers (none of them will be active at this point; disposed flag will cause then to finish so that we can join them)
waitForThreads = true;
}
}
if (waitForThreads)
{
foreach (var worker in this._workers)
{
worker.Join();
}
}
}
public void QueueTask(Action task)
{
lock (this._tasks)
{
if (this._disallowAdd) { throw new InvalidOperationException("This Pool instance is in the process of being disposed, can't add anymore"); }
if (this._disposed) { throw new ObjectDisposedException("This Pool instance has already been disposed"); }
this._tasks.AddLast(task);
Monitor.PulseAll(this._tasks); // pulse because tasks count changed
}
}
private void Worker()
{
Action task = null;
while (true) // loop until threadpool is disposed
{
lock (this._tasks) // finding a task needs to be atomic
{
while (true) // wait for our turn in _workers queue and an available task
{
if (this._disposed)
{
return;
}
if (null != this._workers.First && object.ReferenceEquals(Thread.CurrentThread, this._workers.First.Value) && this._tasks.Count > 0) // we can only claim a task if its our turn (this worker thread is the first entry in _worker queue) and there is a task available
{
task = this._tasks.First.Value;
this._tasks.RemoveFirst();
this._workers.RemoveFirst();
Monitor.PulseAll(this._tasks); // pulse because current (First) worker changed (so that next available sleeping worker will pick up its task)
break; // we found a task to process, break out from the above 'while (true)' loop
}
Monitor.Wait(this._tasks); // go to sleep, either not our turn or no task to process
}
}
task(); // process the found task
this._workers.AddLast(Thread.CurrentThread);
task = null;
}
}
private readonly LinkedList<Thread> _workers; // queue of worker threads ready to process actions
private readonly LinkedList<Action> _tasks = new LinkedList<Action>(); // actions to be processed by worker threads
private bool _disallowAdd; // set to true when disposing queue but there are still tasks pending
private bool _disposed; // set to true when disposing queue and no more tasks are pending
}
public static class Program
{
static void Main()
{
using (var pool = new Pool(5))
{
var random = new Random();
Action<int> randomizer = (index =>
{
Console.WriteLine("{0}: Working on index {1}", Thread.CurrentThread.Name, index);
Thread.Sleep(random.Next(20, 400));
Console.WriteLine("{0}: Ending {1}", Thread.CurrentThread.Name, index);
});
for (var i = 0; i < 40; ++i)
{
var i1 = i;
pool.QueueTask(() => randomizer(i1));
}
}
}
}
}
在运行代码大约两天后,我遇到以下异常:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at System.Collections.Generic.LinkedList`1.InternalInsertNodeBefore(LinkedListNode`1 node, LinkedListNode`1 newNode)
at System.Collections.Generic.LinkedList`1.AddLast(T value)
at MyProg.Pool.Worker()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
我无法找出原因,因为我无法再次获得此错误。有没有关于如何解决这个问题的建议?我想我找到了问题所在。代码示例缺少一个
lock()
锁应该围绕此扩展或包装。\u workers.AddLast(Thread.CurrentThread)代码>
如果查看修改LinkedList
(Pool.QueueTask)的其他代码,它将被包装在锁中 对\u workers
链接列表的访问似乎没有正确同步。考虑这种情况:
让我们假设在某个点上,此.uWorkets
列表包含一项
第一个线程调用this.\u workers.AddLast(thread.CurrentThread)代码>但在一个非常特殊的位置被中断-在AddLast()内方法:
public void AddLast(LinkedListNode<T> node)
{
this.ValidateNewNode(node);
if (this.head == null)
{
this.InternalInsertNodeToEmptyList(node);
}
else
{
// here we got interrupted - the list was not empty,
// but it would be pretty soon, and this.head becomes null
// InternalInsertNodeBefore() does not expect that
this.InternalInsertNodeBefore(this.head, node);
}
node.list = (LinkedList<T>) this;
}
public void AddLast(LinkedListNode节点)
{
this.ValidateNewNode(节点);
if(this.head==null)
{
this.InternalInsertNodeToEmptyList(节点);
}
其他的
{
//这里我们被打断了-列表不是空的,
//但它很快就会变成空的
//InternalInsertNodeBefore()不希望
this.InternalInsertNodeBefore(this.head,node);
}
node.list=(LinkedList)这个;
}
其他线程调用this.\u workers.RemoveFirst()代码>。该语句周围没有lock()
,所以它完成了,现在列表为空AddLast()
现在应该调用InternalInsertNodeToEmptyList(节点)代码>,但无法执行,因为条件已被评估
在单个this.\u workers.AddLast()
行周围放置一个简单的锁(this.\u tasks)
,可以防止出现这种情况
其他不好的情况包括由两个线程同时将项添加到同一列表。堆栈跟踪指向this.\u workers.AddLast(Thread.CurrentThread)代码>作为罪魁祸首。我在C#中与LinkedList的合作不多,但也许它在顺序上出错了,并且不是线程安全的。几乎所有NullReferenceException
的情况都是一样的。请参阅“”以获取一些提示。我认为您遇到了NULLReferenceException错误,因为您尚未首先初始化或正确创建对象。尝试使用新建创建对象
-这可能有助于+1谢谢。这似乎解决了问题。如果我再次观察到这一点,我将进行更新。+1感谢您抽出时间。将语句封装在锁中解决了问题。
private void Worker()
{
Action task = null;
while (true) // loop until threadpool is disposed
{
lock (this._tasks) // finding a task needs to be atomic
{
while (true) // wait for our turn in _workers queue and an available task
{
....
}
}
task(); // process the found task
this._workers.AddLast(Thread.CurrentThread);
task = null;
}
}
public void AddLast(LinkedListNode<T> node)
{
this.ValidateNewNode(node);
if (this.head == null)
{
this.InternalInsertNodeToEmptyList(node);
}
else
{
// here we got interrupted - the list was not empty,
// but it would be pretty soon, and this.head becomes null
// InternalInsertNodeBefore() does not expect that
this.InternalInsertNodeBefore(this.head, node);
}
node.list = (LinkedList<T>) this;
}