ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 速记 线程池

速记 线程池

原创 Linux操作系统 作者:kirinri 时间:2009-03-27 13:23:17 0 删除 编辑
早上起的得早,搂了一眼线程池的内容速记下来。
        public static void Main()
        {
            // Queue the task.
            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc2));
            Console.WriteLine("Main thread does some work, then sleeps.");
            // If you comment out the Sleep, the main thread exits before
            // the thread pool task runs.  The thread pool uses background
            // threads, which do not keep the application running.  (This
            // is a simple example of a race condition.)
            Thread.Sleep(10000);
            Console.WriteLine("Main thread exits.");
            Console.ReadLine();
        }
        // This thread procedure performs the task.
        static void ThreadProc(Object stateInfo)
        {
            // No state object was passed to QueueUserWorkItem, so
            // stateInfo is null.
            Thread.Sleep(5000);
            Console.WriteLine("1111111");
        }
        static void ThreadProc2(Object stateInfo)
        {
            // No state object was passed to QueueUserWorkItem, so
            // stateInfo is null.
            Thread.Sleep(3000);
            Console.WriteLine("222222.");
        }
 
Output:
Main thread does some work, then sleeps.
222222.
1111111
Main thread exits.

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12476590/viewspace-580341/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2007-12-21

  • 博文量
    257
  • 访问量
    498625