欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-08-03 11:30:10
首先来看看如何创建线程: console.writeline(process.getcurrentprocess().threads.count); thread t1 = ne...

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

console.writeline(process.getcurrentprocess().threads.count);
thread t1 = new thread(() =>
    {
        thread.sleep(1000);
        thread t = thread.currentthread;
        console.writeline("name: " + t.name);
        console.writeline("managedthreadid: " + t.managedthreadid);
        console.writeline("state: " + t.threadstate);
        console.writeline("priority: " + t.priority);
        console.writeline("isbackground: " + t.isbackground);
        console.writeline("isthreadpoolthread: " + t.isthreadpoolthread);
    })
    {
        name = "thread1",
        priority = threadpriority.highest
    };
t1.start();
console.writeline(process.getcurrentprocess().threads.count);

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

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

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

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

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

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

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

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

new thread((date) => console.writeline(((datetime)date).tostring())).start(datetime.now);

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

new thread(() => add(1, 2)).start();
static void add(int i, int j)
{
    console.writeline(i + j);
}

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

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

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

new thread(() => console.readline()) { isbackground = false }.start();

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

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

bool b = true;
new thread(() =>
{
    while (b)
    {
        i++;
    }
}) { priority = threadpriority.highest }.start();

new thread(() =>
{
    while (b)
    {
        j++;
    }
}) { priority = threadpriority.lowest }.start();

thread.sleep(1000);
b = false;
console.writeline("i: {0}, j: {1}", i, j);

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

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

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

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

thread t2 = new thread(() =>
    {
        try
        {
            while (true)
            {
                console.writeline(thread.currentthread.threadstate);
                thread.sleep(1000);
            }
        }
        catch (threadabortexception abortexception)
        {
            console.writeline("catch");
            console.writeline(thread.currentthread.threadstate);
            console.writeline((string)abortexception.exceptionstate);
        }
    });
t2.start();
thread.sleep(2000);
t2.abort("haha");
thread.sleep(100);
console.writeline(t2.threadstate);

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

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

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

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

thread t3 = new thread(() =>
    {
        for (int k = 0; k thread.sleep(100);
            console.write("x");
        }
        console.writeline();
    });

thread t4 = new thread(() =>
{
    for (int k = 0; k thread.sleep(100);
        console.write("y");
    }
    console.writeline();
});

t3.start();
t3.join(timespan.frommilliseconds(500));
t4.start();
console.writeline();

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

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

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

我们设置一个静态字段:

static int threadstaticvalue;

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

new thread(() =>
{
    for (int l = 0; l console.writeline("from {0}: {1}", thread.currentthread.name, threadstaticvalue);
}) { name = "1" }.start();

new thread(() =>
{
    for (int m = 0; m console.writeline("from {0}: {1}", thread.currentthread.name, threadstaticvalue);
}) { name = "2" }.start();

运行几次输出结果如下:

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

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

[threadstatic]
static int threadstaticvalue;

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