当前位置: 移动技术网 > IT编程>开发语言>.net > .net面向对象之多线程(Multithreading)及 多线程高级应用

.net面向对象之多线程(Multithreading)及 多线程高级应用

2017年12月12日  | 移动技术网IT编程  | 我要评论

海王祭下载,下电影的好网站,文徽明习字

在.net面向对象程序设计阶段在线程资源共享中的线程安全和线程冲突的解决方案;多线程同步,使用线程锁和线程通知实现线程同步,具体内容介绍如下:

1、 threadstatic特性

特性:[threadstatic]

功能:指定静态字段在不同线程中拥有不同的值

在此之前,我们先看一个多线程的示例:

我们定义一个静态字段:

 static int num = 0;
 然后创建两个线程进行分别累加:

new thread(() =>
{
 for (int i = 0; i < 1000000; i++)
 ++num;
 console.writeline("来自{0}:{1}", thread.currentthread.name, num);
})
{ name = "线程一" }.start(); 

new thread(() =>
{
 for (int i = 0; i < 2000000; i++)
 ++num;
 console.writeline("来自{0}:{1}", thread.currentthread.name, num);
})
{ name = "线程二" }.start();

运行多次结果如下:

    

可以看到,三次的运行结果均不相同,产生这种问题的原因是多线程中同步共享问题导致的,即是多个线程同时共享了一个资源。如何解决上述问题,最简单的方法就是使用静态字段的threadstatic特性。

在定义静态字段时,加上[threadstatic]特性,如下:

复制代码 代码如下:

 [threadstatic]
static int num = 0;
两个线程不变的情况下,再次运行,结果如下:

 

不论运行多少次,结果都是一样的,当字段被threadstatic特性修饰后,它的值在每个线程中都是不同的,即每个线程对static字段都会重新分配内存空间,就当然于一次new操作,这样一来,由于static字段所产生的问题也就没有了。

2. 资源共享

多线程的资源共享,也就是多线程同步(即资源同步),需要注意的是线程同步指的是线程所访问的资源同步,并非是线程本身的同步。

在实际使用多线程的过程中,并非都是各个线程访问不同的资源。

下面看一个线程示例,假如我们并不知道线程要多久完成,我们等待一个固定的时间(假如是500毫秒):

先定义一个静态字段:

 static int result;
创建线程:

thread mythread = new thread(() =>
{
 thread.sleep(1000);
 result = 100;
});
mythread.start();
thread.sleep(500); 
console.writeline(result);

运行结果如下:

 

可以看到结果是0,显然不是我们想要的,但往往在线程执行过程中,我们并不知道它要多久完成,能不能在线程完成后有一个通知?

这里有很多笨的方法,比如我们可能会想到使用一个循环来检测线程状态,这些都不是理想的。

.net为我们提供了一个join方法,就是线程阻塞,可以解决上述问题,我们使用stopwatch来记时,

改进线程代码如下:

system.diagnostics.stopwatch watch = system.diagnostics.stopwatch.startnew();
thread mythread = new thread(() =>
{
 thread.sleep(1000);
 result = 100;
});
mythread.start();
thread.sleep(500);
mythread.join();
console.writeline(watch.elapsedmilliseconds);
console.writeline(result);

运行结果如下:

 结果和我们想要的是一致的。

3. 线程锁

除了上面示例的方法,对于线程同步,.net还为我们提供了一个锁机制来解决同步,再次改进上面示例如下:

先定义一个静态字段来存储锁:

static object locker = new object();
这里我们可以先不用考虑这个对象是什么。继续看改进后的线程:

system.diagnostics.stopwatch watch = system.diagnostics.stopwatch.startnew();
thread t1 = new thread(() =>
{
 lock (locker)
 {
 thread.sleep(1000);
 result = 100;
 }
});
t1.start();
thread.sleep(100);
lock (locker)
{
 console.writeline("线程耗时:"+watch.elapsedmilliseconds);
 console.writeline("线程输出:"+result);
}

运行结果如下:

运行结果和上面示例一样,如果线程处理过程较复杂,可以看到耗时明显减少,这是一种用比阻塞更效率的方式完成线程同步。

4. 线程通知

前面说到了能否在一个线程完成后,通知等待的线程呢,这里.net为我们提供了一个事件通知的方法来解决这个问题。

4.1 autoresetevent 

先定义一个通知对象

复制代码 代码如下:

 static eventwaithandle tellme = new autoresetevent(false);

改进上面的线程如下:

system.diagnostics.stopwatch watch = system.diagnostics.stopwatch.startnew();
thread mythread = new thread(() =>
{
 thread.sleep(1000);
 result = 100;
 tellme.set();
});
mythread.start();
tellme.waitone();
console.writeline("线程耗时:" + watch.elapsedmilliseconds);
console.writeline("线程输出:" + result);

 运行结果如下:

4.2 manualresetevent

和autoresetevent 相对的还有一个 manualresetevent 手动模式,他们的区别在于,在线程结束后manualresetevent 还是可以通行的,除非手动reset关闭。下面看一个示例:

先定义一个手动通知的对象:

static eventwaithandle mre = new manualresetevent(false);


创建线程:

system.diagnostics.stopwatch watch = system.diagnostics.stopwatch.startnew();
thread mythreadfirst = new thread(() =>
{
 thread.sleep(1000);
 result = 100;
 mre.set();
}) { name = "线程一" };
thread mythreadsecond = new thread(() =>
{
 mre.waitone();
 console.writeline(thread.currentthread.name + "获取结果:" + result + "("+system.datetime.now.tostring()+")");
}) { name="线程二"};
mythreadfirst.start();
mythreadsecond.start();
mre.waitone();
console.writeline("线程耗时:" + watch.elapsedmilliseconds + "(" + system.datetime.now.tostring() + ")");
console.writeline("线程输出:" + result + "(" + system.datetime.now.tostring() + ")");

运行结果如下:

4.3. semaphore

semaphore也是线程通知的一种,上面的通知模式,在线程开启的数量很多的情况下,使用reset()关闭时,如果不使用sleep休眠一下,很有可能导致某些线程没有恢复的情况下,某一线程提前关闭,对于这种很难预测的情况,.net提供了更高级的通知方式semaphore,可以保证在超多线程时不会出现上述问题。

先定义一个通知对象的静态字段:

复制代码 代码如下:

   static semaphore sem = new semaphore(2, 2);

使用循环创建100个线程:


for (int i = 1; i <= 100; i++)
{
 new thread(() =>
 {
 sem.waitone();
 thread.sleep(30);
 console.writeline(thread.currentthread.name+" "+datetime.now.tostring());
 sem.release();
 }) { name="线程"+i}.start();
}

运行结果如下:

 

可以看到完整的输出我们所想要看到的结果。

5. 本节要点:

a.线程中静态字段的threadstatic特性,使用该字段在不同线程中拥有不同的值

b.线程同步的几种方式,线程锁和线程通知

c.线程通知的两种方式:autoresetevent /manualresetevent  和 semaphore

到此为止.net面向对象之多线程(multithreading)及多线程高级应用介绍到此为止。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网