码迷,mamicode.com
首页 > 编程语言 > 详细

线程池

时间:2015-01-24 18:52:09      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:线程池

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            int a;
            int b;
            ThreadPool.GetMaxThreads(out a, out b);
            Console.WriteLine("辅助线程最大:{0}。IO线程最大:{1}",a,b);
            for (int i = 0; i < 5; i++)
            {
                ThreadPool.QueueUserWorkItem(Getmsg, i);
            }
            Console.ReadKey();
        }
 
        private static void Getmsg(object o)
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("i:{0}-{1};线程ID:{2}", o.ToString(), i,Thread.CurrentThread.ManagedThreadId); 
            }
        }
    }
}

 

线程池使用起来很简单,但是有一些限制:

①:线程吃池中的所有线程都是后台线程。如果进程所有的前台线程都结束了,所有的后台线程就会停止。不能把如池的线程改为前台线程

②:不能给入池的线程设置优先级或名称

③:对于COM对象,入池的所有线程都是多线程单元线程,许多COM对象都需要单线程单元线程

④:入池的线程只能用于时间较短的任务。对于时间较长的任务可以用Thread类

 

本文出自 “程序猿的家--Hunter” 博客,请务必保留此出处http://962410314.blog.51cto.com/7563109/1607720

线程池

标签:线程池

原文地址:http://962410314.blog.51cto.com/7563109/1607720

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!