2011-09-29 19 views
4

私はCLRのスライスが動作するように、各AppDomainThreadPool時間を与えることを知って、まだ私はそうThread t = new Thread(...);スレッドはスレッドプールによって管理されていますか?

のような新しいスレッドを作成することによってかどうかを知りたかったのは、それはCLRまたはAppDomin ThreadPoolによって管理されていますか?

答えて

3

Threadクラスのスレッドを作成すると、が制御されます。必要に応じて作成し、background or foreground(呼び出しプロセスを生き続ける)かどうかを定義し、Priorityを設定して開始し、停止します。 ThreadPoolまたはTask

(舞台裏ThreadPoolを使用している)あなたはThreadPoolクラスは、スレッドの作成を管理し、あなたに新しいスレッドを作成するために必要な時間を節約し、スレッドの再利用性を、最大限にしましょう。注目すべき点の1つは、Threadのデフォルトとは異なり、ThreadPoolによって作成されたスレッドは呼び出しプロセスを生き残らないということです。

ThreadPoolを使用することの大きな利点は、少数のスレッドで多くのタスクを処理できることです。逆に、スレッドが(再利用性のために設計されているため)スレッドを強制終了しないと仮定すると、ThreadPoolで作成されたスレッドが残っていても、後でアイテム数が減少すると、リソースが無駄になります。

6

Thread t = new Thread();ThreadPoolで管理されません。しかし、オペレーティングシステムのスレッド上でCLRによって提供される抽象化です。 ThreadPoolは、スレッドの再利用とスレッドリソースの共有を容易にする付加的な抽象化です。 http://www.albahari.com/threading/

あなたは.NET 4.0を使用している場合は、TPLを使用して検討してください。

はここ.NETでのスレッドの優れたリソースです。

+0

を入れて、私は、そのスレッドがスレッドプールで管理されていないことを言ったとチームリーダーは私が間違っていたと述べました。今質問は私が彼を修正するかです:) – guyl

+0

素晴らしいリンク、ありがとう! –

1

新しいスレッドを作成すると、ではなく、がスレッドプールによって管理されます。

1

スレッドを手動で作成し、その有効期間を制御する場合、これはスレッドプールとは独立しています。

0

次の例は、スレッドプールの使用方法を示しています。最初にManualResetEventオブジェクトを作成します。これにより、スレッドプールがすべての作業項目の実行を完了したことをプログラムが認識できるようになります。次に、1つのスレッドをスレッドプールに追加しようとします。これが成功すると、残りの部分が追加されます(この例では4つ)。スレッドプールは、作業項目を使用可能なスレッドに入れます。 eventXのWaitOneメソッドが呼び出され、残りのプログラムはイベントがトリガーされるまで(eventX.Setメソッドを使用して)待機します。最後に、スレッド上の負荷(実際に特定のワークアイテムを実行するスレッド)を出力します。

// SimplePool.cs 
// Simple thread pool example 
using System; 
using System.Collections; 
using System.Threading; 

// Useful way to store info that can be passed as a state on a work item 
public class SomeState 
{ 
    public int Cookie; 
    public SomeState(int iCookie) 
    { 
     Cookie = iCookie; 
    } 
} 

public class Alpha 
{ 
    public Hashtable HashCount; 
    public ManualResetEvent eventX; 
    public static int iCount = 0; 
    public static int iMaxCount = 0; 
    public Alpha(int MaxCount) 
    { 
     HashCount = new Hashtable(MaxCount); 
     iMaxCount = MaxCount; 
    } 

    // Beta is the method that will be called when the work item is 
    // serviced on the thread pool. 
    // That means this method will be called when the thread pool has 
    // an available thread for the work item. 
    public void Beta(Object state) 
    { 
     // Write out the hashcode and cookie for the current thread 
     Console.WriteLine(" {0} {1} :", Thread.CurrentThread.GetHashCode(), 
     ((SomeState)state).Cookie); 
     // The lock keyword allows thread-safe modification 
     // of variables accessible across multiple threads. 
     Console.WriteLine(
     "HashCount.Count=={0}, Thread.CurrentThread.GetHashCode()=={1}", 
     HashCount.Count, 
     Thread.CurrentThread.GetHashCode()); 
     lock (HashCount) 
     { 
     if (!HashCount.ContainsKey(Thread.CurrentThread.GetHashCode())) 
      HashCount.Add (Thread.CurrentThread.GetHashCode(), 0); 
     HashCount[Thread.CurrentThread.GetHashCode()] = 
      ((int)HashCount[Thread.CurrentThread.GetHashCode()])+1; 
     } 

     // Do some busy work. 
     // Note: Depending on the speed of your machine, if you 
     // increase this number, the dispersement of the thread 
     // loads should be wider. 
     int iX = 2000; 
     Thread.Sleep(iX); 
     // The Interlocked.Increment method allows thread-safe modification 
     // of variables accessible across multiple threads. 
     Interlocked.Increment(ref iCount); 
     if (iCount == iMaxCount) 
     { 
     Console.WriteLine(); 
     Console.WriteLine("Setting eventX "); 
     eventX.Set(); 
     } 
    } 
} 

public class SimplePool 
{ 
    public static int Main(string[] args) 
    { 
     Console.WriteLine("Thread Pool Sample:"); 
     bool W2K = false; 
     int MaxCount = 10; // Allow a total of 10 threads in the pool 
     // Mark the event as unsignaled. 
     ManualResetEvent eventX = new ManualResetEvent(false); 
     Console.WriteLine("Queuing {0} items to Thread Pool", MaxCount); 
     Alpha oAlpha = new Alpha(MaxCount); // Create the work items. 
     // Make sure the work items have a reference to the signaling event. 
     oAlpha.eventX = eventX; 
     Console.WriteLine("Queue to Thread Pool 0"); 
     try 
     { 
     // Queue the work items, which has the added effect of checking 
     // which OS is running. 
     ThreadPool.QueueUserWorkItem(new WaitCallback(oAlpha.Beta), 
      new SomeState(0)); 
     W2K = true; 
     } 
     catch (NotSupportedException) 
     { 
     Console.WriteLine("These API's may fail when called on a non-Windows 2000 system."); 
     W2K = false; 
     } 
     if (W2K) // If running on an OS which supports the ThreadPool methods. 
     { 
     for (int iItem=1;iItem < MaxCount;iItem++) 
     { 
      // Queue the work items: 
      Console.WriteLine("Queue to Thread Pool {0}", iItem); 
      ThreadPool.QueueUserWorkItem(new WaitCallback(oAlpha.Beta),new SomeState(iItem)); 
     } 
     Console.WriteLine("Waiting for Thread Pool to drain"); 
     // The call to exventX.WaitOne sets the event to wait until 
     // eventX.Set() occurs. 
     // (See oAlpha.Beta). 
     // Wait until event is fired, meaning eventX.Set() was called: 
     eventX.WaitOne(Timeout.Infinite,true); 
     // The WaitOne won't return until the event has been signaled. 
     Console.WriteLine("Thread Pool has been drained (Event fired)"); 
     Console.WriteLine(); 
     Console.WriteLine("Load across threads"); 
     foreach(object o in oAlpha.HashCount.Keys) 
      Console.WriteLine("{0} {1}", o, oAlpha.HashCount[o]); 
     } 
     return 0; 
    } 
} 

アウトは、私は就職の面接で、.NET 4 を使用しています

Thread Pool Sample: 
Queuing 10 items to Thread Pool 
Queue to Thread Pool 0 
Queue to Thread Pool 1 
... 
... 
Queue to Thread Pool 9 
Waiting for Thread Pool to drain 
98 0 : 
HashCount.Count==0, Thread.CurrentThread.GetHashCode()==98 
100 1 : 
HashCount.Count==1, Thread.CurrentThread.GetHashCode()==100 
98 2 : 
... 
... 
Setting eventX 
Thread Pool has been drained (Event fired) 

Load across threads 
101 2 
100 3 
98 4 
102 1 
関連する問題