Task.Factory.StartNew 保证执行顺序
Task.Factory.StartNew guaranteed execution sequence
我有 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,但我不确定这是否是解决方案。
编辑:
在不透露太多关于我继承的程序的情况下 details/drama,我想我正在寻找的是一个自定义 TaskScheduler,它:
代表排队时遵循顺序
串行执行它们
在同一个线程中执行,类似于我上面链接的 StaTaskScheduler 源代码
Does the TaskScheduler guarantee that SomeMethodInThreadA gets executed first before SomeMethodInThreadB?
没有
If not, how do I do this?
你的第二个任务是第一个任务的延续吗ContinueWith
类似于:
var task = Task.Run(() => {
SomeMethodInThreadA();
});
然后:
task.ContinueWith(t => {
SomeOtherMethodInThreadA();
});
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Task.Factory.StartNew(() => MethodA())
.ContinueWith(a =>
{
MethodB();
});
Console.WriteLine("Press a key to exit");
Console.ReadKey(false);
}
public static void MethodA()
{
Thread.Sleep(2000);
Console.WriteLine("Hello From method A {0}", Thread.CurrentThread.ManagedThreadId);
}
public static void MethodB()
{
Thread.Sleep(1000);
Console.WriteLine("Hello From method B {0}", Thread.CurrentThread.ManagedThreadId);
}
}
}
Does the TaskScheduler guarantee that SomeMethodInThreadA gets
executed first before SomeMethodInThreadB?
当然不是。
If not, how do I do this?
如果 2 个线程彼此完全独立,但 SomeMethodInThreadB
必须在 SomeMethodInThreadA
完成后执行,您可以使用标准线程同步例程:
public static AutoResetEvent Wait = new AutoResetEvent(false);
然后在开始第一个任务时:
var task = Task.Run(() =>
{
// Sets the state of the event to nonsignaled, causing threads to block.
Wait.Reset();
// Execute the first task
SomeMethodInThreadA();
// The first task has finished executing -> signal the second
// thread which is probably waiting that it can start processing
// the second task
Wait.Set();
});
然后在第二个线程中等待第一个方法执行完毕的信号:
Task.Run(() =>
{
if (!Wait.WaitOne(TimeSpan.FromMinutes(30)))
{
// We have waited for too long the first task to execute - giving up
}
else
{
// The first task has finished executing - we can now
// proceed with the second task
SomeMethodInThreadB();
}
});
显然,由您来定义第二个线程在放弃之前等待第一个任务完成的时间。
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
}
}
}
}
当没有工作要做时,线程睡眠,阻塞在 foreach 上,一旦您将一个项目添加到队列,它就会得到处理,然后返回睡眠。
我有 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,但我不确定这是否是解决方案。
编辑:
在不透露太多关于我继承的程序的情况下 details/drama,我想我正在寻找的是一个自定义 TaskScheduler,它:
代表排队时遵循顺序
串行执行它们
在同一个线程中执行,类似于我上面链接的 StaTaskScheduler 源代码
Does the TaskScheduler guarantee that SomeMethodInThreadA gets executed first before SomeMethodInThreadB?
没有
If not, how do I do this?
你的第二个任务是第一个任务的延续吗ContinueWith
类似于:
var task = Task.Run(() => {
SomeMethodInThreadA();
});
然后:
task.ContinueWith(t => {
SomeOtherMethodInThreadA();
});
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Task.Factory.StartNew(() => MethodA())
.ContinueWith(a =>
{
MethodB();
});
Console.WriteLine("Press a key to exit");
Console.ReadKey(false);
}
public static void MethodA()
{
Thread.Sleep(2000);
Console.WriteLine("Hello From method A {0}", Thread.CurrentThread.ManagedThreadId);
}
public static void MethodB()
{
Thread.Sleep(1000);
Console.WriteLine("Hello From method B {0}", Thread.CurrentThread.ManagedThreadId);
}
}
}
Does the TaskScheduler guarantee that SomeMethodInThreadA gets executed first before SomeMethodInThreadB?
当然不是。
If not, how do I do this?
如果 2 个线程彼此完全独立,但 SomeMethodInThreadB
必须在 SomeMethodInThreadA
完成后执行,您可以使用标准线程同步例程:
public static AutoResetEvent Wait = new AutoResetEvent(false);
然后在开始第一个任务时:
var task = Task.Run(() =>
{
// Sets the state of the event to nonsignaled, causing threads to block.
Wait.Reset();
// Execute the first task
SomeMethodInThreadA();
// The first task has finished executing -> signal the second
// thread which is probably waiting that it can start processing
// the second task
Wait.Set();
});
然后在第二个线程中等待第一个方法执行完毕的信号:
Task.Run(() =>
{
if (!Wait.WaitOne(TimeSpan.FromMinutes(30)))
{
// We have waited for too long the first task to execute - giving up
}
else
{
// The first task has finished executing - we can now
// proceed with the second task
SomeMethodInThreadB();
}
});
显然,由您来定义第二个线程在放弃之前等待第一个任务完成的时间。
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
}
}
}
}
当没有工作要做时,线程睡眠,阻塞在 foreach 上,一旦您将一个项目添加到队列,它就会得到处理,然后返回睡眠。