ThreadPool thread Pool

Source: Internet
Author: User

1.getmaxthreads,getminthreads
    classProgram {Static voidMain (string[] args) {            intworkerthreads; intcompleteportsthreads; Threadpool.getmaxthreads ( outWorkerThreads, outcompleteportsthreads); Console.WriteLine ("Max threads in thread pool {0}, maximum number of async IO threads in thread pool {1}", WorkerThreads, completeportsthreads); Threadpool.getminthreads ( outWorkerThreads, outcompleteportsthreads); Console.WriteLine ("The minimum number of threads in the thread pool {0}, the minimum amount of async IO threads in the thread pool {1}", WorkerThreads, completeportsthreads);        Console.read (); }    }

The more threads are not the better
①: With a lot of threads, the more threads are dispatched, the more awkward it may be that a task executes much less time than the thread scheduler spends.
②: We want to know that a thread consumes 1M of stack space by default, and if 10,230 threads will take up almost 10G of memory space, I would like the normal computer to strike immediately.

2.setmaxtheads,setminthreads
    classProgram {Static voidMain (string[] args) {            intworkerthreads; intcompleteportsthreads; Threadpool.setmaxthreads ( -, -); Threadpool.setminthreads ( -,Ten); Threadpool.getmaxthreads ( outWorkerThreads, outcompleteportsthreads); Console.WriteLine ("Max threads in thread pool {0}, maximum number of async IO threads in thread pool {1}\n", WorkerThreads, completeportsthreads); Threadpool.getminthreads ( outWorkerThreads, outcompleteportsthreads); Console.WriteLine ("The minimum number of threads in the thread pool {0}, the minimum numbers of async IO threads in the thread pool {1}\n", WorkerThreads, completeportsthreads);        Console.read (); }    }

3.QueueUserWorkItem

The method that needs to accommodate the task and execute it, which has a waitcallback delegate, we simply throw the task that will be executed to the delegate, and the CLR will deploy the idle thread to execute in the threads pool.

    class program    {        staticvoid Main (string[] args)        {            ThreadPool.QueueUserWorkItem (RUN1);            Console.read ();        }         Static void RUN1 (object  obj)        {            Console.WriteLine (" I am thread {0}, am I a thread in the thread pool?") \ n Answer: {1}", Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread);        }    }

Maybe we also need to take some parameters into the worker thread like normal thread, and the second overloaded version of QueueUserWorkItem solves our problem.

    class program    {        staticvoid Main (string[] args)        {            " I am the main thread ");            Console.read ();        }         Static void RUN1 (object  obj)        {            Console.WriteLine (obj);        }    }

4.RegisterWaitForSingleObject

We know that if we throw the task to the thread pool, it is tantamount to pinning our own destiny on someone else's hands.
①: We can no longer control the priority of threads.
②: After losing to the thread pool, we can no longer cancel the task we are going to perform.

Yes, it's up to others to obey the rules of the game, but RegisterWaitForSingleObject provides some simple inter-threading interaction because the first parameter of the method is
WaitHandle, in the VS Object Browser, we find that EventWaitHandle inherits WaitHandle, and ManualResetEvent and AutoResetEvent inherit from the
EventWaitHandle, which means we can dissolve the concept of registerwaitforsingleobject in the signal volume.

    classProgram {Static voidMain (string[] args) {AutoResetEvent ar=NewAutoResetEvent (false); ThreadPool.RegisterWaitForSingleObject (AR, Run1,NULL, Timeout.infinite,false); Console.WriteLine ("time: {0} worker thread please note that you need to wait for 5s to execute. \ n", DateTime.Now); //5sThread.Sleep ( the); Ar.            Set (); Console.WriteLine ("time: {0} worker thread has been executed. \ n", DateTime.Now);        Console.read (); }        Static voidRUN1 (ObjectObjBOOLSign ) {Console.WriteLine ("Current time: {0} I am thread {1}\n", DateTime.Now, Thread.CurrentThread.ManagedThreadId); }    }

We know that there is a timer timer under threading, which is provided by the thread pool and executed when the task is triggered periodically, so we can also implement the function of the timer after the concept of the semaphore is dissolved.

    classProgram {Static voidMain (string[] args) {AutoResetEvent ar=NewAutoResetEvent (false); //parameter 2000: actually is WaitOne (2000), take the timeout mechanismThreadPool.RegisterWaitForSingleObject (AR, RUN1,NULL, -,false);        Console.read (); }        Static voidRUN1 (ObjectObjBOOLSign ) {Console.WriteLine ("Current time: {0} I am thread {1}\n", DateTime.Now, Thread.CurrentThread.ManagedThreadId); }    }

Sometimes, running, we need to stop it at some point, okay, RegisterWaitForSingleObject returns a Registeredwaithandle class, Then we can use the Registeredwaithandle to control the dynamic, such as stop the operation of the counter.

    classProgram {Static voidMain (string[] args) {Registeredwaithandle handle=NULL; AutoResetEvent ar=NewAutoResetEvent (false); //parameter 2000: actually is WaitOne (2000), take the timeout mechanismhandle = ThreadPool.RegisterWaitForSingleObject (AR, RUN1,NULL, -,false); //301Thread.Sleep (10000); Handle.            Unregister (AR); Console.WriteLine ("kill the Thread. ");        Console.read (); }        Static voidRUN1 (ObjectObjBOOLSign ) {Console.WriteLine ("Current time: {0} I am thread {1}\n", DateTime.Now, Thread.CurrentThread.ManagedThreadId); }    }

ThreadPool 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.