它是正确执行? [英] Is it the correct implementation?

查看:200
本文介绍了它是正确执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows服务的需求回暖,从数据库中的工作和需要处理它。

I am having a Windows Service that needs to pick the jobs from database and needs to process it.

下面,各项工作是一个扫描过程中,将需要约10分钟即可完成。

Here, each job is a scanning process that would take approx 10 mins to complete.

我很新的任务并行库。我已经通过以下方式实现为样本的逻辑:

I am very new to Task Parallel Library. I have implemented in the following way as sample logic:

Queue queue = new Queue();

for (int i = 0; i < 10000; i++)
{
    queue.Enqueue(i);
}

for (int i = 0; i < 100; i++)
{
    Task.Factory.StartNew((Object data ) =>
    {
        var Objdata = (Queue)data;
        Console.WriteLine(Objdata.Dequeue());
        Console.WriteLine(
            "The current thread is " + Thread.CurrentThread.ManagedThreadId);
    }, queue, TaskCreationOptions.LongRunning);
}

Console.ReadLine();



但是,这是创造很多线程。的由于循环重复100次,它正在创建100个线程。

是它创建并行线程的数量很多正确的方法?

有没有什么办法的线程数限制为10(并发级别)?

推荐答案

分配新时的一个重要因素要记住主题是操作系统必须分配一个号码 - 一个对象描述

An important factor to remember when allocating new Threads is that the OS has to allocate a number of logical entities in order for that current thread to run:


  1. 线程内核对象:为了逻辑实体,目前的线程运行的螺纹,
    包括线程的上下文,CPU寄存器等

  2. 线程环境块 - 对于异常处理和线程本地
    存储

  3. 用户模式堆栈 - 1MB的堆栈

  4. 内核模式堆栈 - 传递参数从用户模式到内核
    模式

  1. Thread kernel object - an object for describing the thread, including the thread's context, cpu registers, etc
  2. Thread environment block - For exception handling and thread local storage
  3. User-mode stack - 1MB of stack
  4. Kernel-mode stack - For passing arguments from user mode to kernel mode

除此之外,并发数主题可能运行依赖于核心您的机器包装,并创建线程的数量比你的机器拥有将开始引起上下文切换内核数量较大的数量,这从长远来看可能会减慢您的工作了。

Other than that, the number of concurrent Threads that may run depend on the number of cores your machine is packing, and creating an amount of threads that is larger than the number of cores your machine owns will start causing Context Switching, which in the long run may slow your work down.

所以,从长远的介绍后,到好东西。我们真正想要做的是限制运行的线程数量和重用他们尽可能多地。

So after the long intro, to the good stuff. What we actually want to do is limit the number of threads running and reuse them as much as possible.

有关这种工作,我会去的 TPL数据流这是根据生产者 - 消费者模式。只是什么可以做一个小例子:

For this kind of job, i would go with TPL Dataflow which is based on the Producer-Consumer pattern. Just a small example of what can be done:

// a BufferBlock is an equivalent of a ConcurrentQueue to buffer your objects
var bufferBlock = new BufferBlock<object>();

// An ActionBlock to process each object and do something with it
var actionBlock = new ActionBlock<object>(obj =>
{
     // Do stuff with the objects from the bufferblock
});

bufferBlock.LinkTo(actionBlock);
bufferBlock.Completion.ContinueWith(t => actionBlock.Complete());

您可以通过每个阻止 A ExecutionDataflowBlockOptions 这可能会限制在有限容量(在BufferBlock内对象的数量)和 MaxDegreeOfParallelism 告诉块,你可能希望最大的并发数量。

You may pass each Block a ExecutionDataflowBlockOptions which may limit the Bounded Capacity (The number of objects inside the BufferBlock) and MaxDegreeOfParallelism which tells the block the number of maximum concurrency you may want.

有一个很好的例子的此处让你开始。

There is a good example here to get you started.

这篇关于它是正确执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆