Thread pool set up m threads to perform n tasks

Source: Internet
Author: User

Reprint Address: http://www.ithao123.cn/content-4805192.html

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading;namespace            Multithreaded Pool Experiment {class Program {public static void Main () {//New ManualResetEvent object and initialized to no signal state            ManualResetEvent eventx = new ManualResetEvent (false);            Threadpool.setmaxthreads (3, 3);            THR T = new thr (EVENTX); for (int i = 0; i < i++) {ThreadPool.QueueUserWorkItem (new WaitCallback (T.threadproc), I            );             }//waits for the event to complete, that is, the thread calls the Manualresetevent.set () method//eventx.waitone blocks the current thread until the current WaitHandle receives a signal.            Eventx.waitone (Timeout.infinite, true);            Console.WriteLine ("Breakpoint Test");            Thread.Sleep (10000);        Console.WriteLine ("Run End"); } public class Thr {public thr (int count,manualresetevent mre) {IMax                Count = Count;            Eventx = MRE; } public static int iCount = 0;            public static int imaxcount = 0;            public ManualResetEvent Eventx;                public void ThreadProc (Object i) {Console.WriteLine ("thread[" + i.tostring () + "]");                Thread.Sleep (2000); Interlocked.Increment () operation is an atomic operation, the function is: icount++ specific See the following description//atomic operation, is not a higher level interrupt Rob priority operation.                If you ask this question, I'll speak a little deeper.                Because the operating system is in an interrupt state for most of the time,//So a program may be interrupted by a higher priority thread when it executes.                And some operations can not be interrupted, or there will be an inability to restore the consequences, at this time, these operations require atomic operation.                is an operation that cannot be interrupted.                Interlocked.Increment (ref ICount);                    if (ICount = = Imaxcount) {Console.WriteLine ("Send End signal!");                    Sets the event state to the signaled state, allowing one or more waiting threads to continue.                Eventx.set (); }            }        }    }}

  

Reprint Address: http://www.ithao123.cn/content-4805192.html

Thread pool set up m threads to perform n tasks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.