gpt4 book ai didi

c# - 如何在不使用 threadPool 和使用 EventWaitHandler 进行处理的情况下在线程束中运行多个进程

转载 作者:太空宇宙 更新时间:2023-11-03 11:03:29 25 4
gpt4 key购买 nike

  class Process
{
static void Main(string[] args)
{
int threads = 0;
int processes = 0;
Console.WriteLine("Total number of processes:");
processes = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter number of parallel threads:");
threads = Convert.ToInt32(Console.ReadLine());

ManualResetEvent[] events = new ManualResetEvent[threads];

int k = 0, innercount = 0;
//----running in bunches
do
{
for (int l = 0; l < threads; l++)
{
if (k < threads)
{
events[l] = new ManualResetEvent(false);
Runner r = new Runner(events[l], innercount);
new Thread(new ThreadStart(r.Run)).Start();
Console.WriteLine("running start...{0}", innercount);
k++;
innercount++;
}
}
WaitHandle.WaitAny(events);
k--;
Console.WriteLine("Decrement counter...{0}", k);
}
while (innercount < processes);

WaitHandle.WaitAll(events);

Console.WriteLine("All finished!");

Console.ReadLine();

}
}
class Runner
{
static readonly object rngLock = new object();
static Random rng = new Random();

ManualResetEvent ev;
int id;

internal Runner(ManualResetEvent ev, int id)
{
this.ev = ev;
this.id = id;
}

internal void Run()
{

int sleepTime;
lock (rngLock)
{
sleepTime = rng.Next(2000);
}
Thread.Sleep(sleepTime);
Console.WriteLine("Thread Runner {0}",
id);
if (ev.Set())
{
Console.WriteLine("release thread...{0}", id);
}
}
}

我必须运行多个线程。如果一个线程完成,则启动另一个线程。问题是它同时启动了所有进程。(似乎这种情况无法正常工作 WaitHandle.WaitAny(events);)

1:如果有20个线程在运行,当一个线程从20个线程释放时,第21个线程将被启动。

2:不使用线程池可以使用EventWaitHandler完成吗

最佳答案

可以使用 PLinq 和 WithDegreeOfParallelism 做一些事情.

WithDegreeOfParallelism 将限制同时运行的线程数。

以下示例展示了如何使用 Plinq 以有限的并行度运行多个 worker,将不同的对象传递给每个 worker。

它假定您从一系列对象开始,并且您希望将这些对象中的每一个传递给辅助方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace Demo
{
class DataForWorker
{
public int ID;
public string Value;
};

class Program
{
Random rng = new Random();
int numberOfThreadsRunning;

void Run()
{
int maxThreads = 8;

IEnumerable<DataForWorker> dataForWorkers = getDataForWorkers();

dataForWorkers
.AsParallel()
.WithDegreeOfParallelism(maxThreads)
.ForAll(worker);
}

IEnumerable<DataForWorker> getDataForWorkers()
{
// Just return some dummy data.

int numberOfDataItems = 30;

return Enumerable.Range(1, numberOfDataItems).Select
(
n => new DataForWorker
{
ID = n,
Value = n.ToString()
}
);
}

void worker(DataForWorker data)
{
int n = Interlocked.Increment(ref numberOfThreadsRunning);
Console.WriteLine("Thread " + data.ID + " is starting. #threads now = " + n);
Thread.Sleep(rng.Next(1000, 2000));
Console.WriteLine("Thread " + data.ID + " is stopping.");
Interlocked.Decrement(ref numberOfThreadsRunning);
}

static void Main()
{
new Program().Run();
}
}
}

关于c# - 如何在不使用 threadPool 和使用 EventWaitHandler 进行处理的情况下在线程束中运行多个进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16851936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com