.Net中多线程类的使用和总结
lock, Monitor, Thread, Join, BackGroundWorker. 消费者和生产者。Async 委托Invoke
TypeHandle中BlockIndex。
http://msdn.microsoft.com/zh-cn/library/ms173179(v=vs.80).aspx
a. lock相当于 Monitor.Enter, Monitor.Exit. 必须lock引用类型对象。
不能lock公共对象,否则实例将超出代码的控制范围,容易引发死锁。 常见的结构 lock (this)、lock (typeof (MyType)) 和 lock ("myLock") 违反此准则:
对于一段代码锁住a,等待b,另一段代码锁住b,等待a,就会产生死锁
lock(thisLock)
{
if (balance >= amount)
{
Console.WriteLine("Balance before Withdrawal : " + balance);
Console.WriteLine("Amount to Withdraw : -" + amount);
balance = balance - amount;
Console.WriteLine("Balance after Withdrawal : " + balance);
return amount;
}
else
{
return 0; // transaction rejected
}
} lock相当于如下:
if(Monitor.TryEnter(m_inputQueue))
{
try
{
m_inputQueue.Enqueue(qValue);
}
finally
{
Monitor.Exit(m_inputQueue);
}
}
Monitor.Enter(x);
try {
// Code that needs to be protected by the monitor.
}
finally {
// Always use Finally to ensure that you exit the Monitor.
// The following line creates another object containing
// the value of x, and throws SynchronizationLockException
// because the two objects do not match.
Monitor.Exit(x);
}
b. volatile关键字 注意下面的private volatile bool _shouldStop; 加volatitle修饰后可以保证多个线程可以安全的访问此bool变量,这里的用bool表示状态是不需要同步的,并不是volatile保证了其原子性,而仅仅是因为他是个简单的bool型变量。votitle本身并不能保证却对的原子性,如果volatile修饰的是复杂一些结构体,类对象,如果不使用线程同步,会产生非预期的中间状态数据。
using System;
using System.Threading; public class Worker
{
// This method will be called when the thread is started.
public void DoWork()
{
while (!_shouldStop)
{
Console.WriteLine("worker thread: working...");
}
Console.WriteLine("worker thread: terminating gracefully.");
}
public void RequestStop()
{
_shouldStop = true;
}
// Volatile is used as hint to the compiler that this data
// member will be accessed by multiple threads.
private volatile bool _shouldStop;
} public class WorkerThreadExample
{
static void Main()
{
// Create the thread object. This does not start the thread.
Worker workerObject = new Worker();
Thread workerThread = new Thread(workerObject.DoWork); // Start the worker thread.
workerThread.Start();
Console.WriteLine("main thread: Starting worker thread..."); // Loop until worker thread activates.
while (!workerThread.IsAlive); // Put the main thread to sleep for 1 millisecond to
// allow the worker thread to do some work:
Thread.Sleep(); // Request that the worker thread stop itself:
workerObject.RequestStop(); // Use the Join method to block the current thread
// until the object's thread terminates.
workerThread.Join();
Console.WriteLine("main thread: Worker thread has terminated.");
}
}
b. ManualResetEvent
doneEvents[i] = new ManualResetEvent(false); // 1. Set event. Raise singal
doneEvents[i].Set(); // 2. Wait the signal until all the events raise the singal.
WaitHandle.WaitAll(doneEvents);
AutoResetEvent, c. ThreadPool线程池
后台执行任务的线程集合,多用于服务器等需要多个线程的场合。为每个请求分配一个线程来执行请求,执行完请求后线程返回到队列等待再次被使用,避免了每个任务创建/销毁新线程的开销。如果请求过来之后没有可用的线程需要进队列排队。
http://msdn.microsoft.com/zh-cn/library/3dasc8as(v=vs.90).aspx
using System;
using System.Threading; public class Fibonacci
{
public Fibonacci(int n, ManualResetEvent doneEvent)
{
_n = n;
_doneEvent = doneEvent;
} // Wrapper method for use with thread pool.
public void ThreadPoolCallback(Object threadContext)
{
int threadIndex = (int)threadContext;
Console.WriteLine("thread {0} started...", threadIndex);
_fibOfN = Calculate(_n);
Console.WriteLine("thread {0} result calculated...", threadIndex);
_doneEvent.Set();
} // Recursive method that calculates the Nth Fibonacci number.
public int Calculate(int n)
{
if (n <= )
{
return n;
} return Calculate(n - ) + Calculate(n - );
} public int N { get { return _n; } }
private int _n; public int FibOfN { get { return _fibOfN; } }
private int _fibOfN; private ManualResetEvent _doneEvent;
} public class ThreadPoolExample
{
static void Main()
{
const int FibonacciCalculations = ; // One event is used for each Fibonacci object
ManualResetEvent[] doneEvents = new ManualResetEvent[FibonacciCalculations];
Fibonacci[] fibArray = new Fibonacci[FibonacciCalculations];
Random r = new Random(); // Configure and launch threads using ThreadPool:
Console.WriteLine("launching {0} tasks...", FibonacciCalculations);
for (int i = ; i < FibonacciCalculations; i++)
{
doneEvents[i] = new ManualResetEvent(false);
Fibonacci f = new Fibonacci(r.Next(,), doneEvents[i]);
fibArray[i] = f;
ThreadPool.QueueUserWorkItem(f.ThreadPoolCallback, i);
} // Wait for all threads in pool to calculation...
WaitHandle.WaitAll(doneEvents);
Console.WriteLine("All calculations are complete."); // Display the results...
for (int i= ; i<FibonacciCalculations; i++)
{
Fibonacci f = fibArray[i];
Console.WriteLine("Fibonacci({0}) = {1}", f.N, f.FibOfN);
}
}
}
e. AutoResetEvent
.Net中多线程类的使用和总结的更多相关文章
- Delphi中线程类TThread实现多线程编程1---构造、析构……
参考:http://www.cnblogs.com/rogee/archive/2010/09/20/1832053.html Delphi中有一个线程类TThread是用来实现多线程编程的,这个绝大 ...
- 转发 Delphi中线程类TThread 实现多线程编程
Delphi中有一个线程类TThread是用来实现多线程编程的,这个绝大多数Delphi书藉都有说到,但基本上都是对TThread类的几个成员作一简单介绍,再说明一下Execute的实现和Synchr ...
- [转] c#中 多线程访问winform控件
原文 c#中多线程访问winform控件的若干问题小结 我们在做winform应用的时候,大部分情况下都会碰到使用多线程控制界面上控件信息的问题.然而我们并不能用传统方法来解决这个问题,下面我将详细的 ...
- delphi 线程教学第四节:多线程类的改进
第四节:多线程类的改进 1.需要改进的地方 a) 让线程类结束时不自动释放,以便符合 delphi 的用法.即 FreeOnTerminate:=false; b) 改造 Create 的参数 ...
- Java进阶(四十二)Java中多线程使用匿名内部类的方式进行创建3种方式
Java中多线程使用匿名内部类的方式进行创建3种方式 package cn.edu.ujn.demo; // 匿名内部类的格式: public class ThreadDemo { public st ...
- java锁与监视器概念 为什么wait、notify、notifyAll定义在Object中 多线程中篇(九)
在Java中,与线程通信相关的几个方法,是定义在Object中的,大家都知道Object是Java中所有类的超类 在Java中,所有的类都是Object,借助于一个统一的形式Object,显然在有些处 ...
- C#中多线程的并行处理
System.Threading.Tasks,在该命名空间下Task是主类,表示一个类的异步的并发的操作,创建并行代码的时候不一定要直接使用Task类,在某些情况下可以直接使用Parallel静态类( ...
- qt中多线程用法总结
1.多线程的理解 在操作系统中线程和进程划分. 操作系统可以同时执行多个任务,每个任务就是进程:进程可以同时执行多个任务,每个任务就是线程. 线程之间相互独立,抢占式执行.对于单核CPU来说同一时刻只 ...
- 通过编写聊天程序来熟悉python中多线程及socket的用法
1.引言 Python中提供了丰富的开源库,方便开发者快速就搭建好自己所需要的应用程序.本文通过编写基于tcp/ip协议的通信程序来熟悉python中socket以及多线程的使用. 2.python中 ...
随机推荐
- 用swift开发自己的MacOS锁屏软件(二)
上一篇中尝试写了hello world,这一篇中,开始尝试锁屏功能 1.尝试查找swift有没有相关的函数,可以控制系统锁屏之类的,结果并没有找到 2.尝试查找cocoa有没有相关的接口,结果仍然没有 ...
- (转)秒杀系统中如何动态生成下单随机URL
秒杀系统中通常会避免用户之间访问下单页面的URL(避免使用爬虫来造成不公平).所有需要将URL动态化,即使秒杀系统的开发人员也无法在知晓在秒杀开始时的URL.解决办法是在获取秒杀URL的接口中,返回一 ...
- iOS8使用TestFlight进行内部测试功能尝鲜
iOS8发布了有一段时间了,我们的策划很新潮的速度给升级到iOS8了.于是XCode5不支持了,只好从MacOS 10.8升级到10.9,再升级到10.9.5,再下载XCode6安装…… 然后前两天上 ...
- Kubernentes中的身份验证
Kubernentes中的身份验证 kubernetes 系统的各组件需要使用 TLS 证书对通信进行加密,本文档使用 CloudFlare 的 PKI 工具集 cfssl 来生成 Certifica ...
- linux文件传输
Linux下几种文件传输命令 sz rz sftp scp 最近在部署系统时接触了一些文件传输命令,分别做一下简单记录: 1.sftp常用来在两台linux之间传输文件 Secure Ftp 是一个基 ...
- JS方面重点摘要(一)
1.获取样式(1)style只能获取到行间样式的属性(2)火狐getComputedStyle(obj,null)[attr],IE:obj.currentStyle[attr] 2.ready.on ...
- ES里关于数组的拓展
一.静态方法 在ES6以前,创建数组的方式主要有两种,一种是调用Array构造函数,另一种是用数组字面量语法,这两种方法均需列举数组中的元素,功能非常受限.如果想将一个类数组对象(具有数值型索引和le ...
- Linux C存取效率对照——堆、栈、常量区
本文主要探讨堆和栈在使用中的存取效率.利用宏汇编指令分析訪存情况来进行简单推断. 实验环境及使用工具:i686,32位Ubuntu Linux.gcc (Ubuntu/Linaro 4.6.3-1ub ...
- JStorm的搭建文档
1.下载jstorm的jar包 https://github.com/alibaba/jstorm/releases 2.解压jstorm的包 tar -xvf jstorm-2.4.0.tgz mv ...
- axis2 利用小工具cat.aar
Axis2: Web Service是现在最适合实现SOAP的技术,而Axis2是实现Web Service的一种技术框架(架构). 昨天把把菜刀脚本打包发现<>在xml会被转义,导致菜刀 ...