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

Posted mthoutai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用多线程异步创建一组文件相关的知识,希望对你有一定的参考价值。

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

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();

         
        }
    }
}


以上是关于使用多线程异步创建一组文件的主要内容,如果未能解决你的问题,请参考以下文章

单线程多任务异步抓取(asyncio)

Python爬虫之协程,异步协程和多任务异步协程

如何在 C++ 中并行化一个 for 循环,只创建一次线程池

如何创建一组具有在 SwiftUI 中编辑的单独变量的文本字段?

将它们全部单独输出到控制台后,如何创建一组项目?

您如何创建一组与具有特定属性的其他实体有关系的实体?在核心数据中