问题描述
我有如下任务:
- 任务taskInput获取数据inputQueue
- 任务taskOutput从inputQueue数据抓取到outputQueue
- 的消费从outputQueue数据并行任务
我要运行的条件下任务:
相应的code:
//获取数据inputQueue
任务taskInput =新建任务(()=> AddingItemToInputQueue());
taskInput.Start();
//获取数据从inputQueue到outputQueue。
任务taskOutput =新建任务(()=> AddItemToOutputQueue());
taskOutput.Start();
//用于使用来自outputQueue数据并行任务
INT经纬= N;
任务[]工=新任务[经纬]
的for(int i = 0; I<经纬++ I)
{
任务task = Task.Run(()=>消费(一));
工人[我] =任务;
}
Task.WaitAll(工人);
关于inputQueue和outputQueue:
BlockingCollection<邮件> InputQueue =新BlockingCollection<邮件>();
BlockingCollection<邮件> OutputQueue =新BlockingCollection<邮件>();
我的问题:
- 安排的任务。我想我们可以用Task.ContinueWith方法机器人不知道如何将其应用到
消费者
。 - 在我不知道它是否是线程安全的,因为在运行
消费者
新项目可能被添加到inputQueue。
您可以尝试启动第一个任务( taskInput
),当它完成,继续第二个任务( taskOutput
),除非有必要并行工作。在这种情况下,你必须单独启动这两个,因为你已经在做的事情。
Task.Run(()=> AddingItemToInputQueue())
.ContinueWith(任务=> AddItemToOutputQueue());
和并行,开始的任务使用来自 outputQueue
//并行任务
INT经纬= N;
任务[]工=新任务[经纬]
的for(int i = 0; I<经纬++ I)
{
任务task = Task.Run(()=>消费(一));
工人[我] =任务;
}
Task.WaitAll(工人);
或者你可以尝试somehing是这样的:
Task.Run(()=> AddingItemToInputQueue())
.ContinueWith(X => AddItemToOutputQueue())
.ContinueWith(T =>
{
INT经纬= N;
任务[]工=新任务[经纬]
的for(int i = 0; I<经纬++ I)
{
任务task = Task.Run(()=>消费(一));
工人[我] =任务;
}
Task.WaitAll(工人);
});
在这种情况下,它将运行,只要你想:先 taskInput
,然后 taskOutput
最后消费者
。
BlockingCollection 是线程安全的,这样你就可以添加和在多任务中删除数据,它将管理本身就是堵在必要的时候。
您可以查看更多关于 Task.Factory.StartNew
和 Task.Run
的
I have tasks as below:
- Task taskInput to get data to inputQueue
- Task taskOutput to grab data from inputQueue to outputQueue
- Parallel tasks for consuming data from outputQueue
I want to run the tasks with the condition:
The corresponding code:
// Get data to inputQueue
Task taskInput = new Task(()=>AddingItemToInputQueue());
taskInput.Start();
// Grab data from inputQueue to outputQueue.
Task taskOutput = new Task(() => AddItemToOutputQueue());
taskOutput.Start();
// Parallel tasks for consume data from outputQueue
int threadCount = n;
Task[] workers = new Task[threadCount];
for (int i = 0; i < threadCount; ++i)
{
Task task=Task.Run(()=>Consumer(i));
workers[i] = task;
}
Task.WaitAll(workers);
About inputQueue and outputQueue:
BlockingCollection<Messages> InputQueue = new BlockingCollection<Messages>();
BlockingCollection<Messages> OutputQueue = new BlockingCollection<Messages>();
My questions:
- Schedule the tasks. I thought we could use Task.ContinueWith Method bot not sure how to apply it to
Consumer
. - I am not sure whether it is thread safe because the new items may be added to the inputQueue while running the
Consumer
.
You could try starting the first Task (taskInput
) and when it's finished, continue with the second Task (taskOutput
), unless it's necessary to work in parallel. In this case, you have to start both separately, as you're already doing.
Task.Run(() => AddingItemToInputQueue())
.ContinueWith(task => AddItemToOutputQueue());
And in parallel, start Tasks for consume data from outputQueue
// Parallel tasks for consume data from outputQueue
int threadCount = n;
Task[] workers = new Task[threadCount];
for (int i = 0; i < threadCount; ++i)
{
Task task = Task.Run(() => Consumer(i));
workers[i] = task;
}
Task.WaitAll(workers);
Or you could try somehing like this:
Task.Run(() => AddingItemToInputQueue())
.ContinueWith(x => AddItemToOutputQueue())
.ContinueWith(t =>
{
int threadCount = n;
Task[] workers = new Task[threadCount];
for (int i = 0; i < threadCount; ++i)
{
Task task = Task.Run(() => Consumer(i));
workers[i] = task;
}
Task.WaitAll(workers);
});
In this case, it will run as you want: first taskInput
, then taskOutput
and finally Consumer
.
BlockingCollection is thread-safe, so you can add and remove data in multi-tasks, it will manage itself blocking when is necessary.
You can check more about Task.Factory.StartNew
and Task.Run
here
这篇关于为了计划任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!