Execute a Method using the Tread Pool.

Problem: You need to execute a task using a thread from the runtimes thread pool.

Solution: Declare a method containing the code you want to execute. The method’s signature must match that defined by the System.Threading.WaitCallback delegate; that is, it must return void and take a single object argument. Call the static method QueueUserWorkItem of the System.Threading.TreadPool Class, passing it your method name. The runtime will queue your mehtod and execute it when a thread-pool thread becomes available.

How it works: Applications that use many short-lived threads or maintain large numbers of concurrent threads can suffer perfomrmance degradation becaouse of the ovberhead associated with the creation, operation, and destruction of threads. In addition, it is common in multithreaded systems for threads to sit idle a large portion of the time while they wait for the appropriate conditions to trigger their execution. Using a thread pool provides a cmmon solution to improve the scalability, efficiency, and performance of multithreaded systems.

The .NET framework provides a simple thread-pool implentation accessible through the members of the ThreadPool static class. The QueueUserWorkItem method allows you to execute a method using a thread-pool thread by placing a work item on a queue. As a thread from the thread pool becomes available, it takes the next work item from the queue and execute it. The thread performs the work assigned to it, and when it is finished, instead of terminating, the thread returns to the thread pool and takes the next work item from the work queue.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace ConsoleApplication5

Technorati のタグ: ,

{
    class Program
    {

        private class MessageInfo
        {
            private int iterations;
            private string message;

            public MessageInfo(int iterations, string message)
            {
                this.iterations = iterations;
                this.message = message;
            }

            public int Iterations
            {
                get
                {
                    return iterations;
                }
            }

            public string Message
            {
                get
                {
                    return message;
                }
            }
        }
        public static void DisplayMessage(object state)
        {
            MessageInfo config = state as MessageInfo;

            if (config == null)
            {
                for (int count = 0; count < 3; count++)
                {
                    Console.WriteLine("A thread pool example.");

                    Thread.Sleep(1000);
                }
            }
            else
            {
                for (int count = 0; count < config.Iterations; count++)
                {
                    Console.WriteLine(config.Message);

                    Thread.Sleep(1000);
                }
            }
        }

        public static void Main()
        {
            ThreadPool.QueueUserWorkItem(DisplayMessage);

            MessageInfo info = new MessageInfo(5, "A thread pool example with arguments.");

            ThreadPool.QueueUserWorkItem(DisplayMessage, info);

            Console.WriteLine("Main method complete. Press Enter.");
            Console.ReadLine();

        }
    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s