1 Ağustos 2016 Pazartesi

ThreadPool Sınıfı

Giriş
ThreadPool bence modern C# yazılımlarında artık kullanılmaması gereken bir sınıf. Task Parallel Library ile çalışmak daha kolay.

Thread yaratmak için kullanılabilecek şeyler şöyle
C# allows the creation of thread pools by calling on of the following:

- ThreadPool.QueueUserWorkItem
- Asynchronous delegate
- BackgroundWorker
- Task Parallel Library (TPL)
Eksikler
Açıklaması şöyle
Another missing feature in ThreadPool class is signaling when all threads in the thread pool have finished. The ThreadPool class does not expose an equivalent method to Task.WaitAll(), so it must be done manually.
Şöyle yaparız
// Initialize counter with 1, otherwise, the AddCount method will throw an error 
//(the counter is signaled and cannot be incremented).
using (CountdownEvent cde = new CountdownEvent(1))
{
  ManualResetEvent manualResetEvent = new ManualResetEvent(false);
  LibraryAccount account = new LibraryAccount(booksAllowance: booksAllowance);
  for (int i = 0; i < threadsToRun; i++)
  {
    bool enqueued = ThreadPool.QueueUserWorkItem(delegate
    {
      // Increase the counter
      cde.AddCount();
      Trace.WriteLine(message: $"Increase CountDown, current count: {cde.CurrentCount}");
      // Execute the business logic: 
      ...
      // Decrease the counter
      cde.Signal();
      Trace.WriteLine(message: $"Decrease CountDown, current count: {cde.CurrentCount}");
      // Signal that at least one thread is executed.
      manualResetEvent.Set();
    });
  }
  // ensure that at least one thread was created 
  manualResetEvent.WaitOne();

  // Decrease the counter (as it was initialized with the value 1).
  cde.Signal();

  // Wait until the counter is zero.
  cde.Wait();

  Trace.WriteLine(message: $"All threads are finished, current count: {cde.CurrentCount}");
}
QueueUserWorkItems metodu
Açıklaması şöyle
Using the ThreadPool class is the classic clean approach to create a thread in a thread, calling ThreadPool.QueueUserWorkItem method will create a new thread and add it to a queue. After queuing the work-item, the method will run when the thread pool is available; if the thread pool is occupied or reach its upper limit, the thread will wait.
Örnek
Şöyle yaparız.
ThreadPool.QueueUserWorkItem(s =>
{
    SomeMethod();
    DoSomething();
    AndSomeMore();
});
SetMaxThreads metodu
Açıklaması şöyle
The ThreadPool object can be limit the number of threads running under it; it can be configured by the calling ThreadPool.SetMaxThreadmethod. This method's execution is ignored(return False)  if the parameters are lesser than the number of CPUs on the machine. To obtain the number of CPUs of the machine, call Environment.ProcessorCount.
Şöyle yaparız
ThreadPool.SetMaxThreads(5, 1000);
Bu metod ile TPL'in çalışması kontrol edilir. TPL artık belirtilen sayı kadar thread açar.
Task[] tasks = new Task[3] {
    Task.Factory.StartNew(() => ...,
    Task.Factory.StartNew(() => ...,
    Task.Factory.StartNew(() => ...,
    ...
};

Task.WaitAll(tasks);



Hiç yorum yok:

Yorum Gönder