C # Multithreading Management code

Source: Internet
Author: User

    /// <summary>    ///multithreaded Execution/// </summary>     Public classMultithreadingworker {/// <summary>        ///make sure that all threads are complete/// </summary>        PrivateDone M_done =NULL; Private LongM_starttime =0; PrivateQueue m_workload =NULL;  PublicMultithreadingworker () {M_done=NewDone (); M_workload=NewQueue (); }        /// <summary>        ///content Delegation for thread work/// </summary>         PublicWorkcontent Workcontent {Get;Set; } /// <summary>        ///Number of Threads/// </summary>         Public intThreadCount {Get;Set; } Private BOOLM_quit;  Public BOOLQuit {Get{returnM_quit;} Set{M_quit =value;} }        /// <summary>        ///increase the working object to the queue/// </summary>        /// <param name= "work" ></param>         Public voidaddwork (Object work) {Monitor.Enter ( This); M_workload.            Enqueue (work); Monitor.pulse ( This); Monitor.Exit ( This); }        /// <summary>        ///fetching objects from the queue/// </summary>        /// <returns></returns>        PrivateObject obtainwork () {Monitor.Enter ( This);  while(M_workload. Count <1) {monitor.wait ( This); } Object Next=M_workload.            Dequeue (); Monitor.Exit ( This); returnNext; }        /// <summary>        ///Start Execution/// </summary>         Public voidStart () {m_starttime=System.DateTime.Now.Ticks; M_done.            Reset (); M_quit=false;  for(inti =1; I <= threadcount; i++) {ThreadStart TS=NewThreadStart ( This.                Process); Thread M_thread=NewThread (TS); M_thread.            Start (); } m_done.            Waitbegin (); M_done.        Waitdone (); }        /// <summary>        ///Stop Execution/// </summary>         Public voidStop () {m_quit=true; }        Private voidProcess () { while(m_quit==false) {Object workobj=obtainwork (); M_done.                Workerbegin (); Try{workcontent (workobj); }                Catch(Exception e) {System.Console.WriteLine ("Error:"+e.message); }                finally{M_done.                Workerend (); }            }        }    }    /// <summary>    ///execute content applicable delegate/// </summary>    /// <param name= "work" ></param>     Public Delegate voidworkcontent (Object work);  Public classDone {Private intM_activethreads =0; Private BOOLm_started =false;  Public voidWaitdone () {Monitor.Enter ( This);  while(m_activethreads>0) {monitor.wait ( This); } monitor.exit ( This); }         Public voidWaitbegin () {Monitor.Enter ( This);  while( !m_started) {monitor.wait ( This); } monitor.exit ( This); }        /// <summary>        ///Execution Start/// </summary>         Public voidWorkerbegin () {Monitor.Enter ( This); M_activethreads++; M_started=true; Monitor.pulse ( This); Monitor.Exit ( This); }        /// <summary>        ///execution Complete/// </summary>         Public voidWorkerend () {Monitor.Enter ( This); M_activethreads--; Monitor.pulse ( This); Monitor.Exit ( This); }        /// <summary>        ///re-initialize/// </summary>         Public voidReset () {Monitor.Enter ( This); M_activethreads=0; Monitor.Exit ( This); }    }

Calling code:

 Public classtestmultithreading { Public Static voidTest () {Multithreadingworker thwork=NewMultithreadingworker (); Thwork.threadcount= -; Thwork.workcontent=Newworkcontent (Workfun);  for(inti =0; I < -; i++) thwork.addwork (i);        Thwork.start (); }        Private Static voidWorkfun (Objectobj) {Console.WriteLine ("What to do:"+obj.        ToString ()); }    }

C # Multithreading Management code

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.