首页 > 代码库 > 浅谈.NET下的多线程和并行计算(二)线程基本知识

浅谈.NET下的多线程和并行计算(二)线程基本知识

首先来看看如何创建线程:

<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Console</span>.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Process</span>.GetCurrentProcess().Threads.Count);
<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread </span>t1 = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">new </span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread</span>(() =>
    {
        <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread</span>.Sleep(1000);
        <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread </span>t = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread</span>.CurrentThread;
        <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Console</span>.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(163, 21, 21); ">"Name: " </span>+ t.Name);
        <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Console</span>.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(163, 21, 21); ">"ManagedThreadId: " </span>+ t.ManagedThreadId);
        <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Console</span>.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(163, 21, 21); ">"State: " </span>+ t.ThreadState);
        <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Console</span>.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(163, 21, 21); ">"Priority: " </span>+ t.Priority);
        <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Console</span>.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(163, 21, 21); ">"IsBackground: " </span>+ t.IsBackground);
        <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Console</span>.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(163, 21, 21); ">"IsThreadPoolThread: " </span>+ t.IsThreadPoolThread);
    })
    {
        Name = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(163, 21, 21); ">"Thread1"</span>,
        Priority = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">ThreadPriority</span>.Highest
    };
t1.Start();
<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Console</span>.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Process</span>.GetCurrentProcess().Threads.Count);

我们在Thread的构造方法中传入一个Lambda表达式,对应ThreadStart委托(无参void返回值的方法)来构造一个线程任务。这段程序中有几个注意点:

1)从输出结果中可以看到,当前程序启动后就3三个线程,新开线程后显示为4个线程,在线程方法中休眠了一秒,防止主线程执行完次线程就过早结束了。精心开发5年的UI前端框架!

2)我们可以为线程设置一个名字,方便调试。我们也可以设置线程的优先级,这个在之后会有进一步介绍。

3)第7行,托管线程的唯一标识符,微软建议使用托管线程的Id而不是操作系统中线程的Id来跟踪线程。

4)第10行代码输出了当前线程不是后台线程,也就是是前台线程,这是默认值。进程会等待前台线程结束结束,而如果是后台线程的话,所有前台线程结束后后台线程自动终止。对于Windows GUI应用程序来说,使用后台线程很可能发生诡异现象,也就是在程序从任务管理器的应用程序一栏中消失后其进程还在,只能通过手动终止进程来释放内存。

5)第11行代码表明这个线程不是由线程池创建的,有关线程池见后文的介绍。

 image

那么我们再来看看如何为线程传入参数,一种方式是使用匹配ParameterizedThreadStart委托(object参数void返回值)的方法:

<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">new </span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread</span>((date) => <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Console</span>.WriteLine(((<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">DateTime</span>)date).ToString())).Start(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">DateTime</span>.Now);

由于参数是object类型的,我们在使用的时候不得不进行转换,而且还有一个问题就是不支持多个参数,如果要多个参数的话只能使用自定义的对象进行包装,我们还可以使用另外一种方法,那就是使用一个无参方法来包装线程方法主体:

<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">new </span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread</span>(() => Add(1, 2)).Start();
<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">static void </span>Add(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">int </span>i, <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">int </span>j)
{
    <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Console</span>.WriteLine(i + j);
}

上述几行代码的运行结果如下:

image 

再来看一下后台线程前台线程:

<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">new </span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread</span>(() => <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Console</span>.ReadLine()) { IsBackground = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">false </span>}.Start();

这是默认情况,可以看到控制台一直在等待用户的输入,按回车后程序结束,如果把IsBackground属性设置为true的话,可以看到程序在运行后马上接结束了,并没有等待线程方法的结束。精心开发5年的UI前端框架!

之前说过线程的优先级属性,我们做一个实验:

<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">bool </span>b = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">true</span>;
<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">new </span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread</span>(() =>
{
    <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">while </span>(b)
    {
        i++;
    }
}) { Priority = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">ThreadPriority</span>.Highest }.Start();

<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">new </span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread</span>(() =>
{
    <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">while </span>(b)
    {
        j++;
    }
}) { Priority = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">ThreadPriority</span>.Lowest }.Start();

<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread</span>.Sleep(1000);
b = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">false</span>;
<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Console</span>.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(163, 21, 21); ">"i: {0}, j: {1}"</span>, i, j);

开启两个线程做的事情很简单,累加一个静态变量的值,一个优先级最高,一个优先级最低,然后让主线程等待1秒输出结果:

image 

从结果中可以看到,优先级高的线程得到运行的次数比优先级低的线程多那么一点,但即使是最低优先级的线程都有很大的机会来执行。

现在再来看看线程的中断:

<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread </span>t2 = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">new </span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread</span>(() =>
    {
        <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">try
        </span>{
            <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">while </span>(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">true</span>)
            {
                <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Console</span>.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread</span>.CurrentThread.ThreadState);
                <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread</span>.Sleep(1000);
            }
        }
        <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">catch </span>(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">ThreadAbortException </span>abortException)
        {
            <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Console</span>.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(163, 21, 21); ">"catch"</span>);
            <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Console</span>.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread</span>.CurrentThread.ThreadState);
            <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Console</span>.WriteLine((<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">string</span>)abortException.ExceptionState);
        }
    });
t2.Start();
<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread</span>.Sleep(2000);
t2.Abort(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(163, 21, 21); ">"haha"</span>);
<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread</span>.Sleep(100);
<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Console</span>.WriteLine(t2.ThreadState);

在线程方法中,我们1秒输出一次线程的状态,然后主线程休眠2秒后中断线程,略微等待一点时间,等线程中断结束后再获取一次线程的状态。可以看到:

image

每一秒出现一次Running,2秒后由于线程中断处罚ThreadAbortException进入catch块,此时线程的状态是AbortRequested,也能接受到我们中断线程时传入的状态信息,最后线程的状态为Stopped。精心开发5年的UI前端框架!

现在再来看看线程的Join,用于阻塞调用线程等Join的线程完成,或传入一个时间,阻塞一定的时间:

<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread </span>t3 = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">new </span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread</span>(() =>
    {
        <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">for </span>(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">int </span>k = 0; k < 10; k++)
        {
            <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread</span>.Sleep(100);
            <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Console</span>.Write(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(163, 21, 21); ">"X"</span>);
        }
        <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Console</span>.WriteLine();
    });

<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread </span>t4 = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">new </span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread</span>(() =>
{
    <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">for </span>(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">int </span>k = 0; k < 10; k++)
    {
        <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread</span>.Sleep(100);
        <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Console</span>.Write(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(163, 21, 21); ">"Y"</span>);
    }
    <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Console</span>.WriteLine();
});

t3.Start();
t3.Join(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">TimeSpan</span>.FromMilliseconds(500));
t4.Start();
<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Console</span>.WriteLine();

这里可以看到,启动t3之后,我们让主线程阻塞500毫秒,这样的话t3应该已经输出若干X了,然后我们启动t4,随后的500毫秒,t3和t4交替输出X和Y,最后500毫秒由于t3已经结束,所以只会输出Y:

 image

最后,再来看一个有趣的问题:

我们设置一个静态字段:

<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">static int </span>threadstaticvalue;

然后创建两个线程来循环累加这个值:

<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">new </span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread</span>(() =>
{
    <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">for </span>(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">int </span>l = 0; l < 100000; l++)
    {
        threadstaticvalue++;
    }
    <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Console</span>.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(163, 21, 21); ">"from {0}: {1}"</span>, <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread</span>.CurrentThread.Name, threadstaticvalue);
}) { Name = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(163, 21, 21); ">"1" </span>}.Start();

<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">new </span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread</span>(() =>
{
    <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">for </span>(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">int </span>m = 0; m < 200000; m++)
    {
        threadstaticvalue++;
    }
    <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Console</span>.WriteLine(<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(163, 21, 21); ">"from {0}: {1}"</span>, <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">Thread</span>.CurrentThread.Name, threadstaticvalue);
}) { Name = <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(163, 21, 21); ">"2" </span>}.Start();

运行几次输出结果如下:

imageimage image image

虽然我们在代码中指定了两个线程分别累加值10万次和20万次,但是可以看到输出结果五花八门!这是因为两个线程都访问了共享的静态字段,可能错开访问可能正巧同步。其实,在静态字段上加上一个ThreadStatic特性就可以解决:精心开发5年的UI前端框架!

[<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: rgb(43, 145, 175); ">ThreadStatic</span>]
<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.8; color: blue; ">static int </span>threadstaticvalue;

线程同步这个话题很大,我们下次接着讨论。

浅谈.NET下的多线程和并行计算(二)线程基本知识