博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用多线程异步创建一组文件
阅读量:6072 次
发布时间:2019-06-20

本文共 6765 字,大约阅读时间需要 22 分钟。

使用多线程异步创建一组文件

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
FriendList() { return new List
() { 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
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(); } }}

转载地址:http://igngx.baihongyu.com/

你可能感兴趣的文章
linux下查看各硬件型号
查看>>
对象合成复用之策略模式
查看>>
epoll的lt和et模式的实验
查看>>
Flux OOM实例
查看>>
Android的Framework分析---4硬件抽象HAL
查看>>
07-k8s-dns
查看>>
Android 中 ListView 分页加载数据
查看>>
Prawn Notes
查看>>
oracle启动报错:ORA-00845: MEMORY_TARGET not supported on this system
查看>>
Go方法
查看>>
Dapper丶DapperExtention,以及AbpDapper之间的关系,
查看>>
搞IT的同学们,你们在哪个等级__那些年发过的帖子
查看>>
且谈语音搜索
查看>>
MySQL数据库导入导出常用命令
查看>>
centos6.5系统上安装grafana操作步骤
查看>>
低版本Samba无法挂载
查看>>
Date格式转换
查看>>
关于Oracle 数据库的各种不同版本的差别
查看>>
分类信息网络应用
查看>>
beego之orm
查看>>