TaskCreationOptions.LongRunning
ずっと気になっていたので試してみた。
まず普通の 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();
}
}
}
次に 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();
}
}
}
ということで、LongRunning
を指定すれば ThreadPool の利用可能ワーカースレッド数を減らさずに済むようだ。
Author And Source
この問題について(TaskCreationOptions.LongRunning), 我々は、より多くの情報をここで見つけました https://qiita.com/chocolamint/items/3e2e4951ea0fa2ccd19e著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .