Because of the recent need to use multithreading to deal with some problems, at first I used the. NET default ThreadPool, it doesn't feel right. So I realized a simple threadpool myself.
Write a relatively simple, interested friends to see together to improve.
The code is composed primarily of threadpoolex,workitem,workqueue.
Threadpoolex
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading;
6using System.Collections; 7 8namespace Netdragon.threadpoolex 9{public class Threadpoolex {a private workqueue _wo
Rkqueue = new Workqueue ();
public int maxthreadcount = 10;
public int minthreadcount = 2;
Private Hashtable _threadtable = null;
The private int _threadcount = 0;
private int _inuseworkthread = 0;
public double idletimeout = 10; Public Threadpoolex (): This (10,2,2) to public threadpoolex (int max
Threadcouont, int minthreadcount, int idletimeout) Maxthreadcount = Maxthreadcouont;
Minthreadcount = Minthreadcount;
IdleTimeout = idletimeout; _threadtable = hashtable.synchronized (nEW Hashtable (Maxthreadcount));
Panax Notoginseng public void QueueUserWorkItem (WaitCallback waitcallback, object Objparams) 39 {
Enqueueworkitem (WaitCallback, objparams); WaitCallback private void Enqueueworkitem (Waitcallback,object objparams) 44 {45 WorkItem WorkItem = new WorkItem () {workcallback = WaitCallback, 48
Objparams = Objparams 49};
Wuyi _workqueue.push (WorkItem); (_inuseworkthread + _waitworkitem > _threadtable.count) (S)
Tartthread (); Startthread private void () () {_threadtable.c
Ount < Maxthreadcount) {++_threadcount;
The thread thread = new Thread (processworkitems); 66 67Thread.
IsBackground = true; The thread.
Name = "Threadpoolex #" + _threadcount; Thread.
Priority = Threadpriority.normal;
_threadtable[thread] = System.DateTime.Now; A thread.
Start (); Processworkitems () () () ()- {(true) workitem WorkItem = _workqueu
E.pop (); (WorkItem = null) Istimeout = Cu
Rthreadistimeout (); _threadtab if (istimeout) Le.
Count > Minthreadcount) {break; 97} 98} 99 100System.Threading.Thread.Sleep (100); 102 Else, {106
{_threadtable [thread.currentthread] = System.DateTime.Now;
108 Interlocked.Increment (ref _inuseworkthread);
109 Workitem.execute (); 112 catch (Exception) 113 {114 Log something/116 finally 117
{118 interlocked.decrement (ref _inuseworkthread); 119} 121} 122} 123 catch (thre
Adabortexception) 124 {thread.resetabort (); 126} 127 finally 128 {129 if (_threadtable.count > Minthreadcount) 130 {131 _th
Readtable.remove (Thread.CurrentThread); 132} 134} 135 136 private bool Curthreadistimeout () 137 {138
DateTime lastalivetime = (datetime) _threadtable[thread.currentthread];
139 140 DateTime curtime = System.DateTime.Now; Double waitseconds = (curtime-lastalivetime).
TotalSeconds;
143 144 if (Waitseconds > IdleTimeout) 145 {146 return true;
147} 148 149 return false; 152 153 Private int _waitworkitem 154 {151 Get 156 {157
return _workqueue.count;
158} 159} 160 161 public int ThreadCount 162 {163 Get 164 { 165 return _threadtable.coUnt 166} 167} 168} 169} 170