C# |检查线程是否属于托管线程池
线程类负责在多线程编程中创建和管理线程。它提供了一个名为的属性 IsThreadPoolThread 来检查该线程是否属于托管线程池。
语法:
public bool IsThreadPoolThread { get; }
返回值:如果给定线程属于托管线程池,则该属性返回 true 。否则,退回假。该属性的返回类型为系统。布尔。
下面的程序说明了是线程池线程属性的使用:
例 1:
// C# program to illustrate the use
// of IsThreadPoolThread property
using System;
using System.Threading;
class GFG {
// Main Method
static public void Main()
{
Thread T;
// Get the reference of main Thread
// Using CurrentThread property
T = Thread.CurrentThread;
// Check if the main thread belongs to
// the managed thread pool or not
// Using IsThreadPoolThread property
Console.WriteLine("Is main thread belongs to Thread"+
" pool? : {0} ", T.IsThreadPoolThread);
}
}
输出:
Is main thread belongs to Thread pool? : False
例 2:
// C# program to illustrate the use
// of IsThreadPoolThread property
using System;
using System.Threading;
class GFG {
// Main method
public static void Main()
{
// Creating and initializing threads
Thread TR1 = new Thread(new ThreadStart(job));
ThreadPool.QueueUserWorkItem(new WaitCallback(job1));
TR1.Start();
}
// Static methods
public static void job()
{
Console.WriteLine("Is thread 1 belongs to thread pool: {0}",
Thread.CurrentThread.IsThreadPoolThread);
}
public static void job1(object stateInfo)
{
Console.WriteLine("Is thread 2 belongs to thread pool: {0}",
Thread.CurrentThread.IsThreadPoolThread);
}
}
输出:
Is thread 1 belongs to thread pool: False
Is thread 2 belongs to thread pool: True
参考:
版权属于:月萌API www.moonapi.com,转载请注明出处