`
k_lb
  • 浏览: 799491 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论
  • kitleer: 据我所知,国内有款ETL调度监控工具TaskCTL,支持ket ...
    kettle调度

一个使用mutex写的程序。很好理解。。

 
阅读更多

代码系从某论坛上黏贴下来,忘记出处,致歉。。

using System;
using System.Threading;
using System.IO;

public class Class1
{
private static Mutex mut = new Mutex();

public static void Main()
{
Console.WriteLine("请输入启动的线程数: ");
int tc = Convert.ToInt32(Console.ReadLine());
Thread[] thread = new Thread[tc];
for (int i = 0; i < tc; i++)
{
thread[i] = new Thread(new ThreadStart(WriteLog));
thread[i].Name = "Thread " + i.ToString();
}
for (int i = 0; i < tc; i++)
{
thread[i].Start();
}
RL();
}

private static void WriteLog()
{
for (int i = 0; i < 10; i++)
{
mut.WaitOne();
using (StreamWriter writer = new StreamWriter("c://tlog.txt ", true, System.Text.Encoding.UTF8))
{
string txt = Thread.CurrentThread.Name + " write: " + DateTime.Now.ToString();
writer.WriteLine(txt);
writer.Close();
Console.WriteLine(txt);
}
mut.ReleaseMutex();
}
}

private static void RL()
{
Console.ReadLine();
}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics