如何从100个中挑选10个任务,并将其与流程相关联。 [英] How can I pick 10 tasks out of 100 at a time and associate it to process.

查看:114
本文介绍了如何从100个中挑选10个任务,并将其与流程相关联。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的调度程序每分钟运行并获取当时要执行的任务....

目前它创建的线程数与许多任务一样多,因此如果要在6处执行100个任务:00 AM,它在一个taskscheduler.exe进程中创建了100个线程...



它有时非常耗费时间以及长时间运行任务的exe超时在Windows任务调度程序中定义。



我想在同一时刻将多个exe的工作负载分开。

所以而不是一个使用100个线程处理exe(实例),我想创建一批10个线程并跨越10个进程的工作。



这是当前的main()方法

My Scheduler run each minute and fetch tasks to be executed at that time....
Currently it create as much threads as many tasks , so if there are 100 tasks to be executed at 6:00 AM, it creates 100 threads inside a single taskscheduler.exe process...

Its very time consuming sometimes as well the exe timeouts for long running tasks as it defined in Windows task scheduler.

I want to divide the work-load over multiple exe's running at same instant.
So instead of one process exe (instance) with 100 threads , i want to create a batch of 10 threads and span the work over 10 processes.

This is the current main() method

public static void Main()
        {
            Logger logger = LogManager.GetCurrentClassLogger();
            System.Globalization.CultureInfo vCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
            vCulture.DateTimeFormat.FullDateTimePattern = "MM/dd/yyyy hh:mm tt";
            vCulture.DateTimeFormat.ShortDatePattern = "MM/dd/yyyy";
            vCulture.DateTimeFormat.DateSeparator = "/";
            System.Threading.Thread.CurrentThread.CurrentCulture = vCulture;

            DateTime ExecutionStartTime = DateTime.Now;

            ApiTaskComponent apiTaskComponent = new ApiTaskComponent();
            ScheduledTasks.TaskList = apiTaskComponent.GetTaskListToExecute();
            long totalTasksToExecute = ScheduledTasks.TaskCount;
            long maxBatchSize = totalTasksToExecute; ;

            System.Diagnostics.Process current = System.Diagnostics.Process.GetCurrentProcess(); 

            logger.Info("TotalTasksToExecute : " + totalTasksToExecute.ToString());
            logger.Info("MaxBatchSize : " + maxBatchSize.ToString());
            logger.Info("Current Process ID: " + current.Id.ToString());

            if (totalTasksToExecute > 0)
            {
                Thread[] thread = new Thread[totalTasksToExecute];

                for (long i = 0; i < maxBatchSize; i++)
                {
                    Runtask task = new Runtask();
                    thread[i] = new Thread(new ThreadStart(task.RunTaskApi));
                    thread[i].CurrentCulture = vCulture;
                    thread[i].Name = "Thread_" + current.Id.ToString() + "_" + (i + 1).ToString();
                    thread[i].Start();
                }

                for (int j = 0; j < thread.Length; j++)
                {
                    thread[j].Join();
                }
            }

            TimeSpan ElapsedTime = DateTime.Now.Subtract(ExecutionStartTime);

            logger.Info(string.Format("Task Execution Done in {0} Hour(s) {1} Minute(s) and {2} Second(s).", ElapsedTime.Hours.ToString(), ElapsedTime.Minutes.ToString(), ElapsedTime.Seconds.ToString()));
            Console.WriteLine(string.Format("Task Execution Done in {0} Hour(s) {1} Minute(s) and {2} Second(s).", ElapsedTime.Hours.ToString(), ElapsedTime.Minutes.ToString(), ElapsedTime.Seconds.ToString()));
        }
    }





请建议这种方法是否有用!!

我该怎么办?



我的尝试:



我想把工作负载分成多个exe在同一时刻运行。

因此,我想创建一批10个线程并且跨越工作超过10个进程。



Please suggest if this approach will work!!
How can i do this??

What I have tried:

I want to divide the work-load over multiple exe's running at same instant.
So instead of one process exe (instance) with 100 threads , i want to create a batch of 10 threads and span the work over 10 processes.

推荐答案

这不会有任何实际效果 - 事实上,当你创建更多的线程时,它可能会减慢速度新的exe执行也将是线程)。

问题是你只能在系统中同时执行与自由核心一样多的线程:如果你有一个四核处理器,那么你可以真正执行同时有四个线程 - 如果你添加第五个线程,那么系统中的一个线程将停止,直到核心可用(通过线程结束,或者通过它的时间片用完)。启动100个线程并不意味着100个任务将同时发生(尽管它可以像Windows一样是先发制人的多任务系统,并且当线程使用其处理配额时,它会被暂停以给另一个线程一个机会) 。

由于任务切换和管理也增加了开销,因此启动多于线程的线程通常会增加总处理时间。
That's not going to have any real effect - in fact it may slow things down as you are creating even more threads that you were (the new exe executions will also be threads).
The problem is that you can only simultaneously execute as many threads in a system as you have free cores: if you have a four core processor then you can genuinely execute four threads simultaneously - if you add a fifth, then one thread in the system will get halted until a core is available (either by a thread ending, or by it's time slice being used up). Starting 100 threads does not mean that 100 tasks will happen at the same time (though it can appear that way as Windows is a pre-emptive multitasking system and when a thread has used its processing quota it gets suspended to give another thread a chance).
And starting more threads than you can process generally adds to the total processing time as the task switching and management also adds overhead.


这篇关于如何从100个中挑选10个任务,并将其与流程相关联。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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