Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 提高线程效率_C#_Multithreading - Fatal编程技术网

C# 提高线程效率

C# 提高线程效率,c#,multithreading,C#,Multithreading,我有下面的代码,它将读取一个文件的字节,然后构造8个单独的字符串,一旦完成,8个字符串将被拼凑在一起,但当读取一个500mb的文件时,这段代码需要10个多小时,然后我放弃运行它 有没有办法提高这段代码的效率,让它运行得更快 namespace ConsoleApplication1 { public class object1 { public static Byte[] split1 = new Byte[18082460]; public st

我有下面的代码,它将读取一个文件的字节,然后构造8个单独的字符串,一旦完成,8个字符串将被拼凑在一起,但当读取一个500mb的文件时,这段代码需要10个多小时,然后我放弃运行它

有没有办法提高这段代码的效率,让它运行得更快

namespace ConsoleApplication1
{
    public class object1
    {
        public static Byte[] split1 = new Byte[18082460];
        public static Byte[] split2 = new Byte[18082460];
        public static Byte[] split3 = new Byte[18082460];
        public static Byte[] split4 = new Byte[18082460];
        public static Byte[] split5 = new Byte[18082460];
        public static Byte[] split6 = new Byte[18082460];
        public static Byte[] split7 = new Byte[18082460];
        public static Byte[] split8 = new Byte[18082452];

        public static String[] output = new String[8];

        public void run1()
        {
            for (int i = 0; i < split1.Length; i++)
            {
                output[0] += "0x" + split1[i] + ", ";
                Program.amountDone[0] += 1;
                Thread.Sleep(1);
            }
        }
        public void run2()
        {
            for (int i = 0; i < split2.Length; i++)
            {
                output[1] += "0x" + split2[i] + ", ";
                Program.amountDone[1] += 1;
                Thread.Sleep(1);
            }
        }
        public void run3()
        {
            for (int i = 0; i < split3.Length; i++)
            {
                output[2] += "0x" + split3[i] + ", ";
                Program.amountDone[2] += 1;
                Thread.Sleep(1);
            }
        }
        public void run4()
        {
            for (int i = 0; i < split4.Length; i++)
            {
                output[3] += "0x" + split4[i] + ", ";
                Program.amountDone[3] += 1;
                Thread.Sleep(1);
            }
        }
        public void run5()
        {
            for (int i = 0; i < split5.Length; i++)
            {
                output[4] += "0x" + split5[i] + ", ";
                Program.amountDone[4] += 1;
                Thread.Sleep(1);
            }
        }
        public void run6()
        {
            for (int i = 0; i < split6.Length; i++)
            {
                output[5] += "0x" + split6[i] + ", ";
                Program.amountDone[5] += 1;
                Thread.Sleep(1);
            }
        }
        public void run7()
        {
            for (int i = 0; i < split7.Length; i++)
            {
                output[6] += "0x" + split7[i] + ", ";
                Program.amountDone[6] += 1;
                Thread.Sleep(1);
            }
        }
        public void run8()
        {
            for (int i = 0; i < split8.Length; i++)
            {
                if (i == split8.Length)
                {
                    output[7] += "0x" + split8[i];
                }
                else
                {
                    output[7] += "0x" + split8[i] + ", ";
                }
                Program.amountDone[7] += 1;
                Thread.Sleep(1);
            }
        }
    };
    class Program
    {
        public static int curPoint = 0;
        public static object1 obj = new object1();
        public static Thread thread1 = new Thread(new ThreadStart(obj.run1));
        public static Thread thread2 = new Thread(new ThreadStart(obj.run2));
        public static Thread thread3 = new Thread(new ThreadStart(obj.run3));
        public static Thread thread4 = new Thread(new ThreadStart(obj.run4));
        public static Thread thread5 = new Thread(new ThreadStart(obj.run5));
        public static Thread thread6 = new Thread(new ThreadStart(obj.run6));
        public static Thread thread7 = new Thread(new ThreadStart(obj.run7));
        public static Thread thread8 = new Thread(new ThreadStart(obj.run8));
        public static int[] amountDone = { 0, 0, 0, 0, 0, 0, 0, 0 };

        [STAThread]
        static void Main(string[] args)
        {
            Byte[] bytes = GetBytesFromFile(@"C:\Users\JLT\Desktop\320kbTest.rar");

            Array.Copy(bytes, 0, object1.split1, 0, 18082460);
            Array.Copy(bytes, 18082461, object1.split2, 0, 18082460);
            Array.Copy(bytes, 36164922, object1.split3, 0, 18082460);
            Array.Copy(bytes, 54247383, object1.split4, 0, 18082460);
            Array.Copy(bytes, 72329844, object1.split5, 0, 18082460);
            Array.Copy(bytes, 90412305, object1.split6, 0, 18082460);
            Array.Copy(bytes, 108494766, object1.split7, 0, 18082460);
            Array.Copy(bytes, 126577227, object1.split8, 0, 18082452);

            thread1.Start();
            thread2.Start();
            thread3.Start();
            thread4.Start();
            thread5.Start();
            thread6.Start();
            thread7.Start();
            thread8.Start();


            String output = "Byte[] rawData = { ";

            while (true)
            {
                if (thread1.IsAlive || thread2.IsAlive || thread3.IsAlive || thread4.IsAlive || thread5.IsAlive || thread6.IsAlive || thread7.IsAlive || thread8.IsAlive)
                {
                    int temp = 0;
                    foreach (int inter in amountDone)
                    {
                        temp += inter;
                    }
                    Console.WriteLine(temp + " | " + bytes.Length);
                    Thread.Sleep(100);
                    Console.Clear();
                }
                else
                {
                    int temp = 0;
                    foreach (int inter in amountDone)
                    {
                        temp += inter;
                    }
                    if (temp < bytes.Length - 1)
                    {
                        Thread.Sleep(100);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            output += object1.output[0];
            output += object1.output[1];
            output += object1.output[2];
            output += object1.output[3];
            output += object1.output[4];
            output += object1.output[5];
            output += object1.output[6];
            output += object1.output[7];

            output += "};";
            Console.WriteLine(output);
        }

        public static byte[] GetBytesFromFile(string fullFilePath)
        {
            FileStream fs = File.OpenRead(fullFilePath);
            try
            {
                byte[] bytes = new byte[fs.Length];
                fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
                fs.Close();
                return bytes;
            }
            finally
            {
                fs.Close();
            }

        }
    }
}
命名空间控制台应用程序1
{
公共类对象1
{
公共静态字节[]split1=新字节[18082460];
公共静态字节[]split2=新字节[18082460];
公共静态字节[]split3=新字节[18082460];
公共静态字节[]split4=新字节[18082460];
公共静态字节[]split5=新字节[18082460];
公共静态字节[]split6=新字节[18082460];
公共静态字节[]split7=新字节[18082460];
公共静态字节[]split8=新字节[18082452];
公共静态字符串[]输出=新字符串[8];
公共无效运行1()
{
for(int i=0;i