using System;
using System.Threading;
using System.Text;
namespace SigletonPattern.SigletonCounter
{
/**//// <summary>
/// 功能:简单计数器的单件模式
/// 编写:Terrylee
/// 日期:2005年12月06日
/// </summary>
public class CountSigleton
{
/**////存储唯一的实例
static CountSigleton uniCounter = new CountSigleton();
/**////存储计数值
private int totNum = 0;
private CountSigleton()
{
/**////线程延迟2000毫秒
Thread.Sleep(2000);
}
static public CountSigleton Instance()
{
return uniCounter;
}
/**////计数加1
public void Add()
{
totNum ++;
}
/**////获得当前计数值
public int GetCounter()
{
return totNum;
}
} public class CountMutilThread
{
public CountMutilThread()
{
} /**//// <summary>
/// 线程工作
/// </summary>
public static void DoSomeWork()
{
/**////构造显示字符串
string results = ""; /**////创建一个Sigleton实例
CountSigleton MyCounter = CountSigleton.Instance(); /**////循环调用四次
for(int i=1;i<5;i++)
{
/**////开始计数
MyCounter.Add();
results +="线程";
results += Thread.CurrentThread.Name.ToString() + "——〉";
results += "当前的计数:";
results += MyCounter.GetCounter().ToString();
results += "\n"; Console.WriteLine(results);
/**////清空显示字符串
results = "";
}
} public static void Main()
{ Thread thread0 = Thread.CurrentThread;
thread0.Name = "Thread 0";
Thread thread1 =new Thread(new ThreadStart(DoSomeWork));
thread1.Name = "Thread 1";
Thread thread2 =new Thread(new ThreadStart(DoSomeWork));
thread2.Name = "Thread 2";
Thread thread3 =new Thread(new ThreadStart(DoSomeWork));
thread3.Name = "Thread 3";
thread1.Start();
thread2.Start();
thread3.Start();
/**////线程0也只执行和其他线程相同的工作
DoSomeWork(); Console.ReadLine();
}
}} 以上内容运行结果顺序是按照线程0 3 2 1的顺序来显示的
但是把
/**////线程0也只执行和其他线程相同的工作
DoSomeWork();
这句放到实例化线程1前面顺序就是 0 1 2 3.这是为什么啊?线程工作是按照顺序来的吗?
using System.Threading;
using System.Text;
namespace SigletonPattern.SigletonCounter
{
/**//// <summary>
/// 功能:简单计数器的单件模式
/// 编写:Terrylee
/// 日期:2005年12月06日
/// </summary>
public class CountSigleton
{
/**////存储唯一的实例
static CountSigleton uniCounter = new CountSigleton();
/**////存储计数值
private int totNum = 0;
private CountSigleton()
{
/**////线程延迟2000毫秒
Thread.Sleep(2000);
}
static public CountSigleton Instance()
{
return uniCounter;
}
/**////计数加1
public void Add()
{
totNum ++;
}
/**////获得当前计数值
public int GetCounter()
{
return totNum;
}
} public class CountMutilThread
{
public CountMutilThread()
{
} /**//// <summary>
/// 线程工作
/// </summary>
public static void DoSomeWork()
{
/**////构造显示字符串
string results = ""; /**////创建一个Sigleton实例
CountSigleton MyCounter = CountSigleton.Instance(); /**////循环调用四次
for(int i=1;i<5;i++)
{
/**////开始计数
MyCounter.Add();
results +="线程";
results += Thread.CurrentThread.Name.ToString() + "——〉";
results += "当前的计数:";
results += MyCounter.GetCounter().ToString();
results += "\n"; Console.WriteLine(results);
/**////清空显示字符串
results = "";
}
} public static void Main()
{ Thread thread0 = Thread.CurrentThread;
thread0.Name = "Thread 0";
Thread thread1 =new Thread(new ThreadStart(DoSomeWork));
thread1.Name = "Thread 1";
Thread thread2 =new Thread(new ThreadStart(DoSomeWork));
thread2.Name = "Thread 2";
Thread thread3 =new Thread(new ThreadStart(DoSomeWork));
thread3.Name = "Thread 3";
thread1.Start();
thread2.Start();
thread3.Start();
/**////线程0也只执行和其他线程相同的工作
DoSomeWork(); Console.ReadLine();
}
}} 以上内容运行结果顺序是按照线程0 3 2 1的顺序来显示的
但是把
/**////线程0也只执行和其他线程相同的工作
DoSomeWork();
这句放到实例化线程1前面顺序就是 0 1 2 3.这是为什么啊?线程工作是按照顺序来的吗?
解决方案 »
- 正则表达式问题...
- WINDOWS窗体应用程序与WPF应用程序有啥区别?
- winfrom调用jsp传值问题(参与就有分给)
- winform播放声音文件问题,要求对系统资源消耗最少!
- Convert.ToString()与ToString()的区别
- dataGrid1_CurrentCellChanged为什么会这样?
- 请教一个WPF问题
- 请问一个关于用webform的页面打开winform程序的问题
- 请教 winForm的DataGrid绑定DataTable,如何让DataGrid可以编辑行,但不能增加行
- DAL层调用UI层的参数
- C#通过执行Sql语句存储数据,但出现串数问题。
- checkedlistbox多选清空数据库表 (在线等)
如果放在前面 就是相当于同步 必须把它执行完了才往后执行 后面的起用了线程 几乎是同时执行的 你可以执行多次会发现结果会不一样