当前位置:首页 > C#教程 > C#高级

C#学习(七)- Tread

1. 概述与概念

  c#支持通过多线程并行地执行代码,一个线程有它独立的执行路径,能够与其它的线程同时地运行。一个c#程序开始于一个单线程,这个单线程是被clr和操作系统(也称为“主线程”)自动创建的,并具有多线程创建额外的线程。这里的一个简单的例子及其输出:

       using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
using system.threading;

namespace thread_practice
{
    class program
    {
        static void main(string[] args)
        {
            thread t = new thread(writey);
            t.start();                          // run writey on the new thread
            while (true) console.write("x");   // write 'x' forever
        }

        static void writey()
        {
            while (true) console.write("y");   // write 'y' forever
        }
    }
}

程序将不停的输出x和y,且输出x的程序和输出y的程序在不同线程同时运行,因此输出x和y没有次序。输出结果:

  进程之间对于各自内部的变量互不干扰,如:

     using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
using system.threading;

namespace thread_practice
{
    class program
    {
        static void main(string[] args)
        {
            thread t = new thread(write);
            t.start();
            write();
        }

        static void write()
        {
            for (int cycles = 0; cycles < 50; cycles++) console.write("cycles="+cycles+"n");
        }
    }
} }

输出结果:

变量cycles的副本分别在各自的内存堆栈中创建,输出也一样。线程们还可以引用一些公用的目标实例的时候,他们会共享数据。下面是实例:

  using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
using system.threading;

namespace thread_practice
{
    class program
    {
        static bool done = true;
            
        static void main(string[] args)
        {
            thread t = new thread(write);
            t.start();
            thread.sleep(1000);    //让主进程暂停一秒,删除这句可能会由于主进程运行                     //太快而没有来得及察觉到进程t对done值的修改而导致两次输出”it is done!“
            write();
        }

        static void write()
        {
            if (done)
            {
                console.write("it is done!n");
                done = false;
            }
        }
    }
}

输出结果:

输出一个”it is done!“,而不是两个。

关于线程的几个讨论:

线程 vs. 进程

  属于一个单一的应用程序的所有的线程逻辑上被包含在一个进程中,进程指一个应用程序所运行的操作系统单元。

  线程于进程有某些相似的地方:比如说进程通常以时间片方式与其它在电脑中运行的进程的方式与一个c#程序线程运行的方式大致相同。二者的关键区别在于进程 彼此是完全隔绝的。线程与运行在相同程序其它线程共享(堆heap)内存,这就是线程为何如此有用:一个线程可以在后台读取数据,而另一个线程可以在前台展现已读取的数据。

何时使用多线程

  多线程程序一般被用来在后台执行耗时的任务。主线程保持运行,并且工作线程做它的后台工作。对于windows forms程序来说,如果主线程试图执行冗长的操作,键盘和鼠标的操作会变的迟钝,程序也会失去响应。由于这个原因,应该在工作线程中运行一个耗时任务时 添加一个工作线程,即使在主线程上有一个有好的提示“处理中...”,以防止工作无法继续。这就避免了程序出现由操作系统提示的“没有相应”,来诱使用户 强制结束程序的进程而导致错误。模式对话框还允许实现“取消”功能,允许继续接收事件,而实际的任务已被工作线程完成。backgroundworker恰好可以辅助完成这一功能。

  在没有用户界面的程序里,比如说windows service, 多线程在当一个任务有潜在的耗时,因为它在等待另台电脑的响应(比如一个应用服务器,数据库服务器,或者一个客户端)的实现特别有意义。用工作线程完成任务意味着主线程可以立即做其它的事情。

  另一个多线程的用途是在方法中完成一个复杂的计算工作。这个方法会在多核的电脑上运行的更快,如果工作量被多个线程分开的话(使用environment.processorcount属性来侦测处理芯片的数量)。

  一个c#程序称为多线程的可以通过2种方式:明确地创建和运行多线程,或者使用.net framework的暗中使用了多线程的特性——比如backgroundworker类, 线程池,threading timer, 远程服务器,或web services或asp.net程序。在后面的情况,人们别无选择,必须使用多线程;一个单线程的asp.net web server不是太酷,即使有这样的事情;幸运的是,应用服务器中多线程是相当普遍的;唯一值得关心的是提供适当锁机制的静态变量问题。

何时不要使用多线程

  多线程也同样会带来缺点,最大的问题是它使程序变的过于复杂,拥有多线程本身并不复杂,复杂是的线程的交互作用,这带来了无论是否交互是否是有意的,都会 带来较长的开发周期,以及带来间歇性和非重复性的bugs。因此,要么多线程的交互设计简单一些,要么就根本不使用多线程。除非你有强烈的重写和调试欲 望。

当用户频繁地分配和切换线程时,多线程会带来增加资源和cpu的开销。在某些情况下,太多的i/o操作是非常棘手的,当只有一个或两个工作线程要比有众多的线程在相同时间执行任务块的多。稍后我们将实现生产者/耗费者 队列,它提供了上述功能。

2. 创建和开始使用多线程

线程用thread类来创建,通过threadstart委托来指明方法从哪里开始运行,调用start方法后,线程开始运行,线程一直到它所调用的方法返回后结束。下面是一个例子,使用了c#的语法创建theadstart委托:

class threadtest {
    static void main() {
        thread t = new thread (new threadstart (go));
        t.start();   // run go() on the new thread.
        go();        // simultaneously run go() in the main thread.
    }

    static void go() { console.writeline ("hello!"); }    
}

一个线程可以通过c#堆委托简短的语法更便利地创建出来:

 static void main() {
    thread t = new thread (go);    // no need to explicitly use threadstart
    t.start();
    ...
}
static void go() { ... }

在上面这种情况,threadstart被编译器自动推断出来,另一个快捷的方式是使用匿名方法来启动线程:

   static void main() {
    thread t = new thread (delegate() { console.writeline ("hello!"); });
    t.start();
}

将数据传入threadstart中

  如果我们想将参数传入到新的进程中,该怎么办呢? 我们不能使用threadstart委托,因为它不接受参数,既不能这么写:

      static void main(string[] args)
{
    string inputnumber = console.readline();
    int a = int32.parse(inputnumber);

    thread t = new thread(write(a));    //不能以这种方式传递参数到新进程
    t.start();
    write(a);   //main函数中可以这么写
}

static void write(int cycle)
{
    for (int i = 0; i < cycle; i++)
    console.writeline("hello!  i=" + i);
}

  所幸的是,.net framework定义了另一个版本的委托叫做parameterizedthreadstart, 它可以接收一个单独的object类型参数,所以想要传递参数到新进程,我们可以这样:

   static void main(string[] args)
        {
    string inputnumber = console.readline();
    int a = int32.parse(inputnumber);

    thread t = new thread(write);
    t.start(a);     //在这里传入参数
    write(a);
}

static void write(object obj)   //一定传入的是object对象
{
    int cycle = (int)obj;   //强制转换为int
    for (int i = 0; i < cycle; i++)
        console.writeline("hello!  i=" + i);
}

 


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!