development

.NET 앱의 최대 스레드 수?

big-blog 2020. 7. 4. 09:31
반응형

.NET 앱의 최대 스레드 수?


C # 응용 프로그램에서 만들 수있는 최대 스레드 수는 얼마입니까? 이 한계에 도달하면 어떻게됩니까? 어떤 종류의 예외가 발생합니까?


고유 한 제한이 없습니다. 최대 스레드 수는 사용 가능한 물리적 리소스의 양에 따라 결정됩니다. 자세한 내용은 Raymond Chen 의이 기사를 참조하십시오 .

최대 스레드 수를 물어보아야 할 경우 문제가있는 것일 수 있습니다.

[ 업데이트 : 관심의 대상 : .NET 스레드 풀 기본 스레드 수 :

  • Framework 4.0의 1023 (32 비트 환경)
  • Framework 4.0 (64 비트 환경)의 32767
  • Framework 3.5에서 코어 당 250
  • Framework 2.0의 코어 당 25 개

(이 수치는 하드웨어 및 OS에 따라 다를 수 있습니다)]


미치가 맞아. 리소스 (메모리)에 따라 다릅니다.

Raymond의 기사는 C # 스레드가 아닌 Windows 스레드 전용이지만 논리는 동일하게 적용됩니다 (C # 스레드는 Windows 스레드에 매핑 됨).

그러나 C #에서와 같이 완전히 정확하려면 "시작된"스레드와 "시작되지 않은"스레드를 구분해야합니다. 시작된 스레드 만 실제로 스택 공간을 예약합니다 (예상 할 수 있음). 시작되지 않은 스레드는 스레드 오브젝트에 필요한 정보 만 할당합니다 (실제 멤버에 관심이있는 경우 리플렉터를 사용할 수 있음).

실제로 직접 테스트 해 볼 수 있습니다.

    static void DummyCall()
    {
        Thread.Sleep(1000000000);
    }

    static void Main(string[] args)
    {
        int count = 0;
        var threadList = new List<Thread>();
        try
        {
            while (true)
            {
                Thread newThread = new Thread(new ThreadStart(DummyCall), 1024);
                newThread.Start();
                threadList.Add(newThread);
                count++;
            }
        }
        catch (Exception ex)
        {
        }
    }

와:

   static void DummyCall()
    {
        Thread.Sleep(1000000000);
    }

    static void Main(string[] args)
    {
        int count = 0;
        var threadList = new List<Thread>();
        try
        {
            while (true)
            {
                Thread newThread = new Thread(new ThreadStart(DummyCall), 1024);
                threadList.Add(newThread);
                count++;
            }
        }
        catch (Exception ex)
        {
        }
    }

VS의 예외 (물론 메모리 제외)에 중단 점을 두어 카운터 값을 확인하십시오. 물론 매우 중요한 차이가 있습니다.


c # 콘솔을 사용하여 64 비트 시스템에서 테스트를 수행했지만 예외는 2949 스레드를 사용하여 메모리 부족입니다.

I realize we should be using threading pool, which I do, but this answer is in response to the main question ;)


You should be using the thread pool (or async delgates, which in turn use the thread pool) so that the system can decide how many threads should run.


Jeff Richter in CLR via C#:

"With version 2.0 of the CLR, the maximum number of worker threads default to 25 per CPU in the machine and the maximum number of I/O threads defaults to 1000. A limit of 1000 is effectively no limit at all."

Note this is based on .NET 2.0. This may have changed in .NET 3.5.

[Edit] As @Mitch pointed out, this is specific to the CLR ThreadPool. If you're creating threads directly see the @Mitch and others comments.

참고URL : https://stackoverflow.com/questions/145312/maximum-number-of-threads-in-a-net-app

반응형