我有 2 个线程调用“Task.Factory.StartNew”。假设一个线程 (ThreadA) 比另一个线程 (ThreadB) 稍微领先。
... 在线程 A 中,稍微提前调用
Task.Run(() => {
SomeMethodInThreadA();
});
...在线程 B 中,稍后调用
Task.Run(() => {
SomeMethodInThreadB();
});
TaskScheduler 是否保证 SomeMethodInThreadA 在 SomeMethodInThreadB 之前先执行?
如果没有,我该怎么做?使用 Task.Factory.StartNew 并传入特殊的 TaskScheduler?
此外,除了保证 SomeMethodInThreadA 首先得到处理外,我还想确保 SomeMethodInThreadA 在执行 SomeMethodInThreadB 之前先完成。
我研究过使用 StaTaskScheduler ,但我不确定这是否是解决方案。
编辑:
在不透露太多关于我继承的程序的细节/戏剧性的情况下,我想我正在寻找的是一个自定义的 TaskScheduler,它:
在委托(delegate)排队时遵循顺序
串行执行它们
在同一个线程中执行,类似于我上面链接的 StaTaskScheduler 源代码
最佳答案
Without giving out too much details/drama about the program I inherited, I guess what I am looking for is a custom TaskScheduler which:
Honors the sequence when the delegate is queued
Executes them serially
Do it in the same thread, similar to the StaTaskScheduler source code that I linked above
如果这就是您想要的,您需要做的就是制作一个 BlockingCollection<Action>
然后在专用线程上循环遍历列表。
public class BackgroundJobSchedueller
{
readonly BlockingCollection<Action> _queue;
readonly Thread _thread;
public BackgroundJobSchedueller()
{
_queue = new BlockingCollection<Action>()
_thread = new Thread(WorkThread)
{
IsBackground = true,
Name = "Background Queue Processor"
};
_thread.Start();
}
public void StopSchedueller()
{
//Tell GetConsumingEnumerable() to let the user out of the foreach loop
// once the collection is empty.
_queue.CompleteAdding();
//Wait for the foreach loop to finish processing.
_thread.Join();
}
public void QueueJob(Action job)
{
_queue.Add(job);
}
void WorkThread()
{
foreach(var action in _queue.GetConsumingEnumerable())
{
try
{
action();
}
catch
{
//Do something with the exception here
}
}
}
}
当没有工作要做时,线程会 sleep ,阻塞在 foreach 上,一旦您将一个项目添加到队列,它就会得到处理,然后返回 sleep 。
关于c# - Task.Factory.StartNew 保证执行顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34955286/