我做了一个小测试是关于多线程的,但是结果与我期望的不同。
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;namespace ThreadTest
{
public class LockCodeArea
{
private int count = 10;
public void ReduceCount()
{
while (true)
{
lock (this)
{
Console.WriteLine("Current Count:{0}", count);
Console.WriteLine("Current thread HashCode is :{0}", Thread.CurrentThread.GetHashCode());
Thread.Sleep(100);
--count;
if (count > 0)
continue;
else
break;
}
}
}
}
class Program
{
static void Main(string[] args)
{
LockCodeArea testClass = new LockCodeArea();
Thread firsThread = new Thread(new ThreadStart(testClass.ReduceCount));
Thread secondThread = new Thread(new ThreadStart(testClass.ReduceCount));
Console.WriteLine("Main HashCode is :{0}", Thread.CurrentThread.GetHashCode());
Console.WriteLine("First thread Hashcode is :{0}",firsThread.GetHashCode());
Console.WriteLine("Second thread HashCode is :{0}", secondThread.GetHashCode());
firsThread.Start();
secondThread.Start();
if (!firsThread.IsAlive)
Thread.Sleep(1000);
Console.WriteLine("Press Any Key to Continue!");
Console.WriteLine("Main HashCode is :{0}", Thread.CurrentThread.GetHashCode());
Console.ReadLine(); }
}
}
本来是希望使用Lock之后,只存在要么firstThread减数,要么secondThread减数,为什么会有firstThread和secondThead
交替减数的情况。我已经使用Lock锁定代码区了。???各位帮帮忙呀。
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;namespace ThreadTest
{
public class LockCodeArea
{
private int count = 10;
public void ReduceCount()
{
while (true)
{
lock (this)
{
Console.WriteLine("Current Count:{0}", count);
Console.WriteLine("Current thread HashCode is :{0}", Thread.CurrentThread.GetHashCode());
Thread.Sleep(100);
--count;
if (count > 0)
continue;
else
break;
}
}
}
}
class Program
{
static void Main(string[] args)
{
LockCodeArea testClass = new LockCodeArea();
Thread firsThread = new Thread(new ThreadStart(testClass.ReduceCount));
Thread secondThread = new Thread(new ThreadStart(testClass.ReduceCount));
Console.WriteLine("Main HashCode is :{0}", Thread.CurrentThread.GetHashCode());
Console.WriteLine("First thread Hashcode is :{0}",firsThread.GetHashCode());
Console.WriteLine("Second thread HashCode is :{0}", secondThread.GetHashCode());
firsThread.Start();
secondThread.Start();
if (!firsThread.IsAlive)
Thread.Sleep(1000);
Console.WriteLine("Press Any Key to Continue!");
Console.WriteLine("Main HashCode is :{0}", Thread.CurrentThread.GetHashCode());
Console.ReadLine(); }
}
}
本来是希望使用Lock之后,只存在要么firstThread减数,要么secondThread减数,为什么会有firstThread和secondThead
交替减数的情况。我已经使用Lock锁定代码区了。???各位帮帮忙呀。
解决方案 »
- 打开本地文件!!!
- C#做一个登录界面,Combox选择数据服务器
- 怎么写软件变动需求
- [分享]写了一个QQ斗地主辅助工具,通过识别两套扑克牌,分析出对方拥有的扑克牌..
- C#中怎么读取系统的内码,也就是说我的工程如何判断当前操作系统是中文的还是英文的或者日文的。。。
- 关于office开发方面的问题
- 简单的正则表达式写法:限定输入1-10个字符!!
- 有一个文件里面的内容是简体字,我想用一个程序将里面的简体字全部转为繁体字,怎么做啊!
- 急急急!!!关于水晶报表的发布问题
- 如何在FORM最小化时作为一个小图标显示在屏幕右下角?
- C#里可以调用MFC的dll么
- 幫我看看下面代碼,哪裡有錯,急呀。看了好久看不知錯在哪裡。
如果要同一时间只有1个线程能执行整个循环,就要把整个循环(10次)都锁住,也就是说: public void ReduceCount()
{
lock (this)
{
while (true)
{
Console.WriteLine("Current Count:{0}", count);
Console.WriteLine("Current thread HashCode is :{0}", Thread.CurrentThread.GetHashCode());
Thread.Sleep(100);
--count;
if (count > 0)
continue;
else
break;
}
}
}注意,此时还是有2个线程进入了ReduceCount方法,但是只有先进入的线程执行了循环内的整个过程,然后break。这是等在方法口的另一个线程才执行该方法的代码(只有1次,因为count已经是0,超出了条件了)。
所以进一步修改代码,不让最后一次多余的执行显示出来:public void ReduceCount()
{
lock (this)
{
while (true)
{
if (count > 0)
{
Console.WriteLine("Current Count:{0}", count);
Console.WriteLine("Current thread HashCode is :{0}", Thread.CurrentThread.GetHashCode());
Thread.Sleep(100);
--count;
continue;
}
else
break;
}
}
}