. NET parallel computing and concurrency 6-gets the maximum number of threads available for the thread pool

Source: Internet
Author: User

1 usingSystem;2 usingSystem.IO;3 usingSystem.Security.Permissions;4 usingSystem.Threading;5 6 classTest7{8     Static voidMain ()9{TenAutoResetEvent mainevent =NewAutoResetEvent (false); One         intWorkerThreads; A         intPortthreads; -  -Threadpool.getmaxthreads ( outWorkerThreads, outPortthreads); theConsole.WriteLine ("\nmaximum worker threads: \t{0}" + -"\nmaximum Completion Port threads: {1}", -WorkerThreads, portthreads); -  +Threadpool.getavailablethreads ( outWorkerThreads, -              outPortthreads); +Console.WriteLine ("\navailable worker threads: \t{0}" + A"\navailable Completion Port threads: {1}\n", atWorkerThreads, portthreads); -  -ThreadPool.QueueUserWorkItem (New -WaitCallback (Threadpooltest.workitemmethod), mainevent); -  -         //Since ThreadPool threads is background threads, in         //wait for the work item to signal before ending Main. -Mainevent.waitone (5000,false); toConsole.ReadLine (); +} -} the  * classThreadpooltest ${Panax Notoginseng     //maintains state information to is passed to Endwritecallback. -     //This information allows the callback to the end of the asynchronous the     //write operation and signal when it is finished. +     classState A{ the          PublicFileStream FStream; +          PublicAutoResetEvent autoevent; -  $          PublicState (FileStream fstream, AutoResetEvent autoevent) ${ -              This. fstream = FStream; -              This. autoevent = autoevent; the} -}Wuyi  theThreadpooltest () {} -  Wu      Public Static voidWorkitemmethod (ObjectMainevent) -{ AboutConsole.WriteLine ("\nstarting workitem.\n"); $AutoResetEvent autoevent =NewAutoResetEvent (false); -  -         //Create some data. -         Const intArraySize = 10000; A         Const intBufferSize = 10000; +         byte[] ByteArray =NewByte[arraysize]; the         NewRandom (). Nextbytes (ByteArray); -  $         //Create The files and the state objects. theFileStream FileWriter1 = the             NewFileStream (@ "C:\[email Protected]##.dat", FileMode.Create, theFileAccess.ReadWrite, Fileshare.readwrite, theBufferSize,true); -FileStream FileWriter2 = in             NewFileStream (@ "C:\[email Protected]##.dat", FileMode.Create, theFileAccess.ReadWrite, Fileshare.readwrite, theBufferSize,true); Aboutstate StateInfo1 =NewState (FileWriter1, autoevent); thestate StateInfo2 =NewState (FileWriter2, autoevent); the  the         //Asynchronously write to the files. +Filewriter1.beginwrite (ByteArray, 0, Bytearray.length, -             NewAsyncCallback (Endwritecallback), stateInfo1); theFilewriter2.beginwrite (ByteArray, 0, Bytearray.length,Bayi             NewAsyncCallback (Endwritecallback), StateInfo2); the  the         //Wait for the callbacks to signal. -Autoevent.waitone (); -Autoevent.waitone (); the  theFilewriter1.close (); theFilewriter2.close (); theConsole.WriteLine ("\nending workitem.\n"); -  the         //Signal Main , the work item is finished. the((AutoResetEvent) mainevent). Set (); the}94  the     Static voidEndwritecallback (IAsyncResult AsyncResult) the{ theConsole.WriteLine ("starting Endwritecallback.");98  Aboutstate StateInfo = (state) asyncresult.asyncstate; -         intWorkerThreads;101         intPortthreads;102         Try103{104Threadpool.getavailablethreads ( outWorkerThreads, the                  outPortthreads);106Console.WriteLine ("\navailable worker threads: \t{0}" +107"\navailable Completion Port threads: {1}\n",108WorkerThreads, portthreads);109  theStateInfo.fStream.EndWrite (AsyncResult);111  the             //Sleep so the other thread have a chance to run113             //Before the current thread ends. the             //thread.sleep (); the} the         finally117{118             //Signal The current thread is finished.119StateInfo.autoEvent.Set (); -Console.WriteLine ("ending Endwritecallback.");121}122}123}124 

. NET parallel computing and concurrency 6-gets the maximum number of threads available for the thread pool

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.