실제 컴퓨팅 작업과 비슷한 가짜 테스트를 만들었습니다. 현재 코드 :C# 하나의 긴 컴퓨팅 프로세스를 병렬 처리하는 최선의 방법은 무엇입니까?
static void Main()
{
List<ulong> list = new List<ulong>();
Action action =() =>
{
Random rng = new Random(Guid.NewGuid().GetHashCode());
ulong i = 0;
do
{
i++;
if (rng.Next(100000000) == 1000)
{
lock (list) list.Add(i);
Console.WriteLine("ThreadId {0}, step {1}: match is found",
Thread.CurrentThread.ManagedThreadId, i);
}
} while (list.Count < 100);
};
int length = Environment.ProcessorCount;
Action[] actions = new Action[length];
for (int i = 0; i < length; i++)
actions[i] = action;
Parallel.Invoke(actions);
Console.WriteLine("The process is completed. {0} matches are found. Press any key...",
list.Count);
Console.ReadKey();
}
하나의 긴 컴퓨팅 프로세스에 병렬 작업 수를 최적화하는 더 좋은 방법이 있습니까?
새끼 고양이에게 불쾌감을주는'List'를'lock'하는 대신'ConcurrentBag'와 같은 TPL 콜렉션을 체크 아웃해야합니다 – MickyD
@MickyD. [여기] (http://stackoverflow.com/a/29307598/6123485)를 보시고 직접 시도하십시오. 나는 그랬다. – quicktrick
OP에 downvoting. 그러한 경우에 대한 더 나은 접근 방법을 알고 있다면 downvoting 대신 답을주십시오. 나는이 질문을하기 전에 인터넷을 매우 오래 검색했다. 나는 아직 그러한 과제에 대한 더 나은 해결책을 찾지 못했다. – quicktrick