使用多线程异步创建一组文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.IO; using System.Diagnostics; namespace ThreadAppDemo { //mock data class Friend { public string name { get; set; } public string gender { get; set; } public int age { get; set; } public static List<Friend> FriendList() { return new List<Friend>() { new Friend() {name ="aa",gender ="boy",age =24}, new Friend() {name ="bb",gender ="girl",age =22}, new Friend() {name ="cc",gender ="boy",age =23}, new Friend() {name ="dd",gender ="boy",age =25} ,new Friend() {name ="ee",gender ="girl",age =20}, new Friend() {name ="ff",gender ="boy",age =21} ,new Friend() {name ="gg",gender ="girl",age =26}, new Friend() {name ="hh",gender ="girl",age =26} ,new Friend() {name ="ll",gender ="girl",age =26}, new Friend() {name ="mm",gender ="girl",age =26}, new Friend() {name ="nn",gender ="boy",age =26} ,new Friend() {name ="ii",gender ="girl",age =26}, new Friend() {name ="jj",gender ="girl",age =26}, new Friend() {name ="kk",gender ="girl",age =26}, new Friend() {name ="oogg",gender ="girl",age =26}, new Friend() {name ="oo",gender ="girl",age =26}, new Friend() {name ="ppqq",gender ="girl",age =24}, new Friend() {name ="qq",gender ="girl",age =20}, new Friend() {name ="xxx",gender ="girl",age =21}, new Friend() {name ="yy",gender ="girl",age =23}, new Friend() {name ="z",gender ="girl",age =26}, new Friend() {name ="zz",gender ="girl",age =22}, new Friend() {name ="1aa",gender ="boy",age =24}, new Friend() {name ="b1b",gender ="girl",age =22}, new Friend() {name ="cc1",gender ="boy",age =23}, new Friend() {name ="dd2",gender ="boy",age =25} ,new Friend() {name ="1ee",gender ="girl",age =20}, new Friend() {name ="ff1",gender ="boy",age =21} ,new Friend() {name ="1gg",gender ="girl",age =26}, new Friend() {name ="2hh",gender ="girl",age =26} ,new Friend() {name ="3ll",gender ="girl",age =26}, new Friend() {name ="4mm",gender ="girl",age =26}, new Friend() {name ="4nn",gender ="boy",age =26} ,new Friend() {name ="6ii",gender ="girl",age =26}, new Friend() {name ="1jj",gender ="girl",age =26}, new Friend() {name ="k1k",gender ="girl",age =26}, new Friend() {name ="o2ogg",gender ="girl",age =26}, new Friend() {name ="o1o",gender ="girl",age =26}, new Friend() {name ="pp1qq",gender ="girl",age =24}, new Friend() {name ="qq1",gender ="girl",age =20}, new Friend() {name ="1xxx",gender ="girl",age =21}, new Friend() {name ="1yy",gender ="girl",age =23}, new Friend() {name ="z1",gender ="girl",age =26}, new Friend() {name ="1zz",gender ="girl",age =22} }; } } //使用多线程异步的创建一组文件 class WaitAllDemo { //define thread counts const int threadNumber = 5; static void Main() { string dir = @"E:\File"; if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } ManualResetEvent[] manualResetEvent = new ManualResetEvent[threadNumber]; Stopwatch watch = new Stopwatch(); watch.Start(); long startTime = Stopwatch.GetTimestamp(); for (int i = 0; i < threadNumber; i++) { manualResetEvent[i] = new ManualResetEvent(false); FileState state = new FileState() { filePathName = dir, manualResetEvent = manualResetEvent[i] }; Thread thread = new Thread(new ParameterizedThreadStart(Writer.WriteFile)); thread.Name = string.Concat("thread", i + 1); thread.Start(state); //下面可以用线程池代替 // ThreadPool.QueueUserWorkItem(Writer.WriteFile, state); } //当所有的事件都收到信号,WaitALL返回true,主线程开始执行 WaitHandle.WaitAll(manualResetEvent); watch.Stop(); Console.WriteLine("执行此操作耗时:{0}毫秒", (Stopwatch.GetTimestamp() - startTime) / (Stopwatch.Frequency / 1000)); Console.WriteLine("创建完毕,主线程退出"); } } class FileState { public string filePathName { get; set; } public ManualResetEvent manualResetEvent { get; set; } } class Writer { private static List<Friend> friends = Friend.FriendList(); public static void WriteFile(object obj) { FileState state = (FileState)obj; string filePath = null; string fileName = null; //遍历所有数据,创建文件 while (friends.Count > 0) { lock (friends) { if (friends.Count > 0) { Friend friend = friends.First(); fileName = friend.name; filePath = string.Concat(state.filePathName, @"\test", fileName, ".txt"); byte[] byteArray = Encoding.ASCII.GetBytes(string.Format("Name:{0},Gender:{1},Age:{2}", friend.name, friend.gender, friend.age).ToString()); Console.WriteLine("Start Work Item,FileName:{0},ThreadName:{1}", fileName,Thread.CurrentThread.Name); FileStream file = null; try { file = new FileStream(filePath, FileMode.Create); file.Write(byteArray, 0, byteArray.Length); } finally { if (file != null) { file.Close(); } Console.WriteLine("End Work Item,FileName:{0},ThreadName:{1}", fileName, Thread.CurrentThread.Name); } friends.Remove(friends.First()); } } } //当数据处理完毕,调用Set方法,把事件状态更新为收到信号 state.manualResetEvent.Set(); } } }