记录高级编程一些东西

来源:互联网 发布:浏览器打不开淘宝链接 编辑:程序博客网 时间:2024/04/29 01:20

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

线程池中的所有线程都是后台线程,

不能给线程池中的线程设置优先级和名称

入池的线程只能用于时间较短的任务,如果线程要一直运行(如word的拼写检查器线程)就应该使用Thread类

Thread类给线程传递数据可以采用两种方法。一种是使用带ParameterizedThreadStart委托参数的Thread构造参数,另一种方式是创建一个自定义类,把线程的方法定义为实例方法,这样就可以初始化实例的数据,之后启动线程。

方法一:ParameterizedThreadStart

 object d=new Test();
            Thread t2=new Thread(ThreadMainwithParas);
            t2.Start(d);

方法二:

static void Main(string[] args)
        { Test test = new Test("info");
            Thread t = new Thread(test.Job);
            t.Start();

}

public class Test
        {
            private string data;
            public Test(string data)
            { 
                this.data = data;
            }
            public void Job()
            { 
                Console.WriteLine("The info received is {0}", data); 
            }
        }

0 0