3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

TaskCreationOptions.LongRunning

Last updated at Posted at 2013-03-10

ずっと気になっていたので試してみた。

まず普通の Task.Factory.StartNew

Program.normal.cs
using System;
using System.Threading;
using System.Threading.Tasks;

namespace BackgroundTest
{
    class Program
    {
        static void Main(string[] args)
        {
            int workerThreads, completionPortThreads;
            
            ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);
            Console.WriteLine(workerThreads);
            Console.WriteLine(completionPortThreads);
            
            ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
            Console.WriteLine(workerThreads);
            Console.WriteLine(completionPortThreads);

            Console.WriteLine("--");

            Task.Factory.StartNew(() =>
            {
                ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);
                Console.WriteLine(workerThreads);
                Console.WriteLine(completionPortThreads);

                ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
                Console.WriteLine(workerThreads);
                Console.WriteLine(completionPortThreads);

                while (true) { }
            
            });

            Console.Read();
        }
    }
}

GetAvailableThreads の戻り値が 1 減っている

次に TaskCreationOptions.LongRunning を指定。

Program.long.cs
using System;
using System.Threading;
using System.Threading.Tasks;

namespace BackgroundTest
{
    class Program
    {
        static void Main(string[] args)
        {
            int workerThreads, completionPortThreads;
            
            ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);
            Console.WriteLine(workerThreads);
            Console.WriteLine(completionPortThreads);
            
            ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
            Console.WriteLine(workerThreads);
            Console.WriteLine(completionPortThreads);

            Console.WriteLine("--");

            Task.Factory.StartNew(() =>
            {
                ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);
                Console.WriteLine(workerThreads);
                Console.WriteLine(completionPortThreads);

                ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
                Console.WriteLine(workerThreads);
                Console.WriteLine(completionPortThreads);

                while (true) { }
            
            }, TaskCreationOptions.LongRunning);

            Console.Read();
        }
    }
}

GetAvailableThreads の戻り値に変化なし

ということで、LongRunning を指定すれば ThreadPool の利用可能ワーカースレッド数を減らさずに済むようだ。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?