C#中同时执行2个线程 [英] c# execute 2 threads simultaneously
问题描述
我想一个HTTP处理程序中重现线程错误条件。
基本上,ASP.net工人procecss是创建2个线程同时它时调用HTTP处理程序在应用程序中的某一页的负荷。
在HTTP处理程序,是不是线程安全的资源。因此,当2个线程试图同时访问它发生异常。
我可能会,把一个锁声明周围的资源,但是我想确保它是逸岸的情况。所以,我想创建一个控制台应用程序第一次的情况。
但我不能得到2个线程在同一时间像asp.net WP确实执行的方法。所以,我的问题是你怎么可以创建2个线程,可以同时执行一个方法。
编辑:
底层的资源是用户表SQL数据库(只有一个名字列)。下面是一个示例code我试过了。
[识别TestClass]
公共类UnitTest1
{
[测试方法]
公共无效Linq2SqlThreadSafetyTest()
{
VAR threadOne =新主题(新ParameterizedThreadStart(InsertData));
VAR threadTwo =新主题(新ParameterizedThreadStart(InsertData)); threadOne.Start(1); //试图通过这个参数来同步。
threadTwo.Start(0); threadOne.Join();
threadTwo.Join();
}
私有静态无效InsertData(对象毫秒)
{
// LINQ的2 SQL数据上下文
VAR数据库=新DataClassesDataContext(); //数据库实体
VAR NEWUSER =新用户{名称=测试}; database.Users.InsertOnSubmit(NEWUSER); Thread.sleep代码((int)的毫秒级); 尝试
{
database.SubmitChanges(); //这个语句引发的HTTP处理程序例外。
} 赶上(例外的例外)
{
的Debug.WriteLine(exception.Message);
}
}
}
您可以只设置一个静态的时间来启动这样你的工作。
私有静态的DateTime STARTTIME =
DateTime.Now.AddSeconds(5);
//任意启动时间静态无效的主要(字串[] args)
{
的ThreadStart threadStart1 =新的ThreadStart(DoSomething的);
的ThreadStart threadStart2 =新的ThreadStart(DoSomething的);
螺纹TH1 =新主题(threadStart1);
螺纹TH2 =新主题(threadStart2); th1.Start();
th2.Start();
块引用>th1.Join();
th2.Join(); 到Console.ReadLine();
} 私有静态无效DoSomething的()
{
而(DateTime.Now<的startTime)
{
//没做什么
} //两个线程将在这里执行code并行
}
块引用>I am trying to reproduce a threading error condition within an HTTP Handler.
Basically, the ASP.net worker procecss is creating 2 threads which invoke the HTTP handler in my application simultaneously when a certain page loads.
Inside the http handler, is a resource which is not thread safe. Hence, when the 2 threads try to access it simultaneously an exception occurs.
I could potentially, put a lock statement around the resource, however I want to make sure that it is infact the case. So I wanted to create the situation in a console application first.
But i cant get 2 threads to execute a method at the same time like asp.net wp does. So, my question is how can you can create 2 threads which can execute a method at the same time.
Edit:
The underlying resource is a sql database with a user table (has a name column only). Here is a sample code i tried.
[TestClass] public class UnitTest1 { [TestMethod] public void Linq2SqlThreadSafetyTest() { var threadOne = new Thread(new ParameterizedThreadStart(InsertData)); var threadTwo = new Thread(new ParameterizedThreadStart(InsertData)); threadOne.Start(1); // Was trying to sync them via this parameter. threadTwo.Start(0); threadOne.Join(); threadTwo.Join(); } private static void InsertData( object milliseconds ) { // Linq 2 sql data context var database = new DataClassesDataContext(); // Database entity var newUser = new User {Name = "Test"}; database.Users.InsertOnSubmit(newUser); Thread.Sleep( (int) milliseconds); try { database.SubmitChanges(); // This statement throws exception in the HTTP Handler. } catch (Exception exception) { Debug.WriteLine(exception.Message); } } }
解决方案You could just set a static time to start your work like this.
private static DateTime startTime = DateTime.Now.AddSeconds(5); //arbitrary start time
static void Main(string[] args) { ThreadStart threadStart1 = new ThreadStart(DoSomething); ThreadStart threadStart2 = new ThreadStart(DoSomething); Thread th1 = new Thread(threadStart1); Thread th2 = new Thread(threadStart2); th1.Start(); th2.Start();
th1.Join();
th2.Join(); Console.ReadLine(); } private static void DoSomething() { while (DateTime.Now < startTime) { //do nothing } //both threads will execute code here concurrently }
这篇关于C#中同时执行2个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!