답변은 코어 수와 관계없이 전체 병렬 연산의 상한선입니다.
IO 또는 잠금을 기다리고 있기 때문에 CPU를 사용하지 않더라도 여분의 작업은 병렬로 실행되지 않고 사용자가 지정한 최대 작업 만 실행됩니다.
이 부분을 찾으려면이 테스트 코드를 작성하십시오. 거기에 더 많은 스레드를 사용하도록 TPL을 자극하는 인공 잠금 장치가 있습니다. 코드가 IO 또는 데이터베이스를 기다리고있을 때도 마찬가지입니다. 내가 MaxDegreeOfParallelism을 지정하지 않으면
class Program
{
static void Main(string[] args)
{
var locker = new Object();
int count = 0;
Parallel.For
(0
, 1000
, new ParallelOptions { MaxDegreeOfParallelism = 2 }
, (i) =>
{
Interlocked.Increment(ref count);
lock (locker)
{
Console.WriteLine("Number of active threads:" + count);
Thread.Sleep(10);
}
Interlocked.Decrement(ref count);
}
);
}
}
는 콘솔 로깅은 약 8 작업까지 동시에 실행되는 것을 보여줍니다. 이처럼 :
Number of active threads:6
Number of active threads:7
Number of active threads:7
Number of active threads:7
Number of active threads:7
Number of active threads:7
Number of active threads:6
Number of active threads:7
Number of active threads:7
Number of active threads:7
Number of active threads:7
Number of active threads:7
Number of active threads:7
Number of active threads:7
Number of active threads:7
Number of active threads:7
Number of active threads:7
Number of active threads:7
Number of active threads:7
그것은 낮은 시작, 시간이 지남과 끝에 증가는 동시에 8을 실행하는 데 노력하고있다. 내가 어떤 임의의 값으로 제한하는 경우
은 (예를 들어 2), 나는
Number of active threads:2
Number of active threads:1
Number of active threads:2
Number of active threads:2
Number of active threads:2
Number of active threads:2
Number of active threads:2
Number of active threads:2
Number of active threads:2
Number of active threads:2
Number of active threads:2
Number of active threads:2
Number of active threads:2
Number of active threads:2
Number of active threads:2
Number of active threads:2
Number of active threads:2
아를 얻을이는 쿼드 코어 시스템에 있습니다.
내 논리에는 대기 또는 IO가 없으며, SQL을 업데이트합니다. 그렇지만 SQL은 자체적으로 수행 할 수도 있지만 대부분 SQL이 완료 될 때까지 기다리고 있습니다. 활성 스레드의 기본 최대 개수는 얼마입니까? –
코어 당 기본값은 2이지만 코드가 CPU를 사용하지 않는 경우 TPL이이 값을 올릴 수 있습니다. 대부분의 데이터베이스는 일정량의 IO를 필요로합니다. –
6 코어 머신이 과중하게로드되는 경우 1 또는 2 개의 스레드 만 사용합니다. 가볍게로드되면 최대 12 개까지 올라갑니다. 기존 시스템로드를 고려할만큼 지능적입니다. – Contango