C # multithreading simple and easy to use original code

Source: Internet
Author: User

Using system; <br/> using system. threading; <br/> class crunthread <br/>{< br/> static void main () <br/>{< br/> thread. currentthread. name = "================================ main thread ==================== ==== "; <br/> crunthread. printthreadinfo (); <br/> operthreads (); <br/>}< br/> Public static void printthreadinfo () <br/>{< br/> // thread. currentthread. name = "the main thread" + RDM. next (); // name of the current thread <br/> console. writeline (thread. currentthread. name + "/tstatus:" + thread. currentthread. threadstate); <br/>}< br/> Public static void operthreads () <br/>{< br/> console. writeline ("thread start/stop/join sample"); <br/> A = new A ("Hello, andylin :)"); <br/> threadstart srttrd = new threadstart (. run); <br/> thread TRD = new thread (srttrd); <br/> TRD. start (); </P> <p> thread. sleep (10); <br/> TRD. suspend (); <br/> // TRD. abort (); <br/> // TRD. join (); <br/> console. writeline ("/n =======. run has finished ========== "); <br/> try <br/>{< br/> console. writeline ("try to restart. run... "); <br/> TRD. start (); <br/>}< br/> catch (threadstateexception e) <br/>{< br/> console. writeline (E. tostring (); <br/>}< br/> catch (system. exception e) <br/>{< br/> console. writeline (E. tostring (); <br/>}< br/> console. writeline ("============= end of operator thread ================================ "); <br/>}< br/> Class A <br/>{< br/> private string m_strval; </P> <p> Public A (string Str) <br/>{< br/> m_strval = STR; <br/>}< br/> Public void run () <br/>{< br/> thread. currentthread. name = "The a thread"; // name of the current thread <br/> while (true) <br/>{< br/> crunthread. printthreadinfo (); <br/> console. writeline ("strvalue:" + m_strval); <br/> console. writeline (". run is runing in its own thread... "); <br/>}< br/>} 

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.