1. Start thread with thread start
ThreadStart represents a method of executing a thread
ThreadStart (Delegate () {})
public static void Testthread () {int max_thread_count = 10; Long Currentthreadcount = 0; for (var i = 0;i < 100;i + +) {var II = i; while (true) {if (Interlocked.read (ref Currentthreadcount) < Max_thread_count) Break Thread.Sleep (1000); } interlocked.increment (ref currentthreadcount); New Thread (New ThreadStart (delegate () {thread.sleep (1000); Console.WriteLine ("Current position is" + II); Thread.Sleep (1000); Interlocked.decrement (ref Currentthreadcount); })). Start (); } while (True) {Console.WriteLine ("Running thread count" + currentthreadcount); if (Interlocked.read (ref CURRENTTHreadcount) = = 0) break; Thread.Sleep (1000); } }
ThreadStart (method name)
public static void Testthread () { int max_thread_count = 10; long currentthreadcount = 0; &nbs P for (var i = 0;i < 100;i + +) { var II = i; while (true) &N Bsp { if (interlocked). Read (ref Currentthreadcount) < Max_thread_count) and nbsp break; Thread.Sleep (+); } interlocked.increment (ref cur RentthreadCount); New Thread (new ThreadStart (DoWork)). Start (); //new Thread (new ThreadStart (Work.dowork)). Start (); interlocked.decrement (ref Currentthreadcount); & nbsp } while (true) &NBS P { Console.WriteLine ("Running thread count" + currentthreadcount); nbsp if (Interlocked.read (ref currentthreadcount) = = 0) &N Bsp break; Thread.Sleep (1000) ; } } public static void do Work () { &nbsP Console.WriteLine ("Static thread procedure."); } class work { &NBSP ; public static void DoWork () { &NB Sp Console.WriteLine ("Static thread procedure."); } public int data; public void domorework () { Console.WriteLine ("Instance thread procedure. Data={0} ", Data); } }
2. Use ThreadPool QueueUserWorkItem to perform multithreading
public static void Testthread () { int max_thread_count = ten; var semaphore = new Semaphore (Max_thread_count, max_thread_count); for (var i = 0;i < 100;i + +) { var II = i; ThreadPool.QueueUserWorkItem ( (state0) = { semaphore. WaitOne (); Thread.Sleep (+); Console.WriteLine ("Process" + II); Semaphore. Release (); } ); } }
Http://stackoverflow.com/questions/3334178/limit-threads-count
3. Key differences in two methods
The analysis in this is better: Http://stackoverflow.com/questions/6192898/thread-start-versus-threadpool-queueuserworkitem
Starting a new thread can be a very expensive operation. The thread pool reuses threads and thus amortizes the cost. Unless need a dedicated thread, the thread pool is the recommended a-go. By using a dedicated the thread you had more control over thread specific attributes such as priority, culture and so forth. Also, you should does long running tasks on the thread pool as it would force the pool to spawn additional threads.
In addition to the options mention. NET 4 offers some great abstractions for concurrency. The Check out of the Task and Parallel classes as well as the new PLINQ methods.
ThreadPool can reuse threads, thus making it more cost-saving. Thread start is more convenient to use directly.
C # multithreading using THEAD and ThreadPool comparisons