.NET, MultiThreading ve Basit bir ThreadPool Implementasyonu

22-9-2007

Piyasada çok sayıda ThreadPool örneği ve .NET inde kendi içerisinde ThreadPool ve benzeri işleri yapan class'ları var. Ben ufak bir class yazdım ThreadPool' dan öte sadece Thread Blockluyor ve ek olarak Mevcut threadları bir daha kullanmıyor yeni açıyor vs. Belki aradığınız bir şey değil ancak hızlı implemantasyonlarda sizi kastırmaktan kesinlikle koruyor.

Kullanmak Basit :

- Her yeni thread açtığınızda .Open() ile bunu bildirin
- Kapatığınızda .Close() ile bunu bildirin
- Tüm threadları eklediğinizde .AllPushed() ile bunu bildirin
- Yeni thread eklemeden önce WaitForThreads() ile kendinize yeni slot bekleyin
- Eğer threadların hepsi bitince öğrenmek istiyorsanız ki genelde isteyecekseniz ThreadsFinished() event' ını yakalayın.

 

Basit bir kullanım örneği :

   1:  Imports System.Threading
   2:   
   3:  Module ThreadPoolTest
   4:   
   5:      Dim ThreadPool As New ThreadPool(100)
   6:   
   7:      Sub Main()
   8:   
   9:          AddHandler ThreadPool.ThreadsFinished, AddressOf Finish
  10:   
  11:          For i As Integer = 0 To 100000
  12:              ThreadPool.Open()
  13:   
  14:              Dim Thr As New Threading.Thread(New ParameterizedThreadStart(AddressOf RunSomething))
  15:              Thr.Start(i)
  16:   
  17:              ThreadPool.WaitForThreads()
  18:   
  19:          Next i
  20:   
  21:          ThreadPool.AllJobsPushed()
  22:   
  23:      End Sub
  24:   
  25:   
  26:      Private ConsoleLock As New Object
  27:      Public Sub RunSomething(ByVal threadPosition As Object)
  28:   
  29:          Thread.Sleep(100)
  30:          SyncLock ConsoleLock
  31:              Console.WriteLine(threadPosition.ToString)
  32:          End SyncLock
  33:   
  34:          ThreadPool.Close()
  35:   
  36:      End Sub
  37:   
  38:      Private Sub Finish()
  39:   
  40:          SyncLock ConsoleLock
  41:              Console.WriteLine("All threads finished!")
  42:              Console.ReadKey()
  43:          End SyncLock
  44:   
  45:      End Sub
  46:   
  47:  End Module

 

Aşağıdaki de Class' ın kendisi :

   1:  Imports System.Threading
   2:   
   3:  ''' <summary>
   4:  ''' Pretty simple Thread Pool implementation
   5:  ''' </summary>
   6:  ''' <remarks></remarks>
   7:  Public Class ThreadPool
   8:   
   9:  #Region "Properties"
  10:      Private CountLock As New Object
  11:   
  12:      Private _Count As Integer
  13:   
  14:      ''' <summary>
  15:      ''' Current Thread Count 
  16:      ''' </summary>
  17:      ''' <value></value>
  18:      ''' <returns></returns>
  19:      ''' <remarks></remarks>
  20:      Public ReadOnly Property ThreadCount() As Integer
  21:          Get
  22:              SyncLock CountLock
  23:                  Return _Count
  24:              End SyncLock
  25:          End Get
  26:      End Property
  27:   
  28:      Private _MaxThreadCount As Integer
  29:   
  30:      ''' <summary>
  31:      ''' Maximum Thread Count
  32:      ''' </summary>
  33:      ''' <value></value>
  34:      ''' <returns></returns>
  35:      ''' <remarks></remarks>
  36:      Public Property MaxThreadCount() As Integer
  37:          Get
  38:              Return _MaxThreadCount
  39:          End Get
  40:          Set(ByVal value As Integer)
  41:              _MaxThreadCount = value
  42:          End Set
  43:      End Property
  44:   
  45:      ''' <summary>
  46:      ''' New Thread Pool
  47:      ''' </summary>
  48:      ''' <param name="maxThreadCount"></param>
  49:      ''' <remarks></remarks>
  50:      Public Sub New(ByVal maxThreadCount As Integer)
  51:          Me.MaxThreadCount = maxThreadCount
  52:      End Sub
  53:   
  54:   
  55:   
  56:      ''' <summary>
  57:      ''' Is job finished (all threads closed?)
  58:      ''' </summary>
  59:      ''' <value></value>
  60:      ''' <returns></returns>
  61:      ''' <remarks><c>True</c> if all opened threads closed <c>False</c> otherwise</remarks>
  62:      Public ReadOnly Property Finished() As Boolean
  63:          Get
  64:              Return AllPushed AndAlso ThreadCount = 0
  65:          End Get
  66:      End Property
  67:   
  68:   
  69:      Private _Status As ThreadStatus
  70:   
  71:      ''' <summary>
  72:      ''' Thread Status
  73:      ''' </summary>
  74:      ''' <value></value>
  75:      ''' <returns></returns>
  76:      ''' <remarks></remarks>
  77:      Public Property Status() As ThreadStatus
  78:          Get
  79:              Return _Status
  80:          End Get
  81:          Set(ByVal value As ThreadStatus)
  82:              _Status = value
  83:          End Set
  84:      End Property
  85:   
  86:   
  87:      Private PushLock As New Object
  88:      Private _AllPushed As Boolean
  89:   
  90:      ''' <summary>
  91:      ''' All jobs are pushed to ThreadPool
  92:      ''' </summary>
  93:      ''' <value></value>
  94:      ''' <returns></returns>
  95:      ''' <remarks></remarks>
  96:      Private ReadOnly Property AllPushed() As Boolean
  97:          Get
  98:              SyncLock PushLock
  99:                  Return _AllPushed
 100:              End SyncLock
 101:          End Get
 102:      End Property
 103:   
 104:   
 105:  #End Region
 106:   
 107:  #Region "Enums"
 108:      ''' <summary>
 109:      ''' Current status of thread pool
 110:      ''' </summary>
 111:      ''' <remarks></remarks>
 112:      Public Enum ThreadStatus
 113:          Started
 114:          Stopped
 115:          Paused
 116:      End Enum
 117:  #End Region
 118:   
 119:  #Region "Events"
 120:      ''' <summary>
 121:      ''' Inform that there is no more thread in the pool
 122:      ''' </summary>
 123:      ''' <remarks></remarks>
 124:      Public Event ThreadsFinished()
 125:  #End Region
 126:   
 127:  #Region "Thread Management"
 128:      ''' <summary>
 129:      ''' Open a new thread
 130:      ''' </summary>
 131:      Public Sub Open()
 132:          Interlocked.Increment(_Count)
 133:      End Sub
 134:   
 135:      ''' <summary>
 136:      ''' Informs Thread Pool that all jobs pushed
 137:      ''' </summary>
 138:      ''' <remarks></remarks>
 139:      Public Sub AllJobsPushed()
 140:          SyncLock PushLock
 141:              Me._AllPushed = True
 142:          End SyncLock
 143:      End Sub
 144:   
 145:   
 146:      ''' <summary>
 147:      ''' Close one thread
 148:      ''' </summary>
 149:      Public Sub Close()
 150:          Interlocked.Decrement(_Count)
 151:          If Me.Finished Then RaiseEvent ThreadsFinished()
 152:      End Sub
 153:   
 154:      ''' <summary>
 155:      ''' Wait for opened threads before open new ones
 156:      ''' </summary>
 157:      ''' <remarks></remarks>
 158:      Public Sub WaitForThreads()
 159:          While (Me.ThreadCount >= MaxThreadCount) OrElse (Me.Status = ThreadStatus.Paused)
 160:              Threading.Thread.Sleep(20)
 161:          End While
 162:      End Sub
 163:   
 164:  #End Region
 165:   
 166:  End Class

Recent Blog Posts

See all of the blog posts