写文件需要为交通繁忙第3部分进行了优化 [英] Write file need to optimised for heavy traffic part 3

查看:77
本文介绍了写文件需要为交通繁忙第3部分进行了优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是第2部分的延续,谁是兴趣看看我来自哪里,你可以参考第1部分和第2部分,但它是没有必要的。

this question is a continuation of the first 2 part, anyone who is interested to see where I come from you can refer to part 1 and part 2, but it is not necessary.

写文件需要为交通繁忙优化

写文件需要为交通繁忙优化部分2

现在我有一个工作片断,相关部分如下:

now i have a working snippet, the relevant part is below:

    public static class memoryStreamClass
    {
        static MemoryStream ms1 = new MemoryStream();

        public static void fillBuffer(string outputString)
        {
            byte[] outputByte = Encoding.ASCII.GetBytes(outputString);

            ms1.Write(outputByte, 0, outputByte.Length);

            if (ms1.Length > 8100)
            {
                emptyBuffer(ms1);
                ms1.SetLength(0);
                ms1.Position = 0;
            }
        }

        static void emptyBuffer(MemoryStream ms)
        {
            FileStream outStream = new FileStream("c:\\output.txt", FileMode.Append);

            ms.WriteTo(outStream);
            outStream.Flush();
            outStream.Close();
        }



上面的代码工作正常,并没有缺陷。它的输出数据左右8KB的每一个写操作。

the above snippet works fine, and bug free. it output around 8KB of data every write.

现在我尽量多线程上面的代码,增强了IO瓶颈写入和问题的表现出现了。下面的代码片段是我想尝试。

now i try to multithread the above code to enhance the performance of an IO write bottleneck and problems appeared. the below snippet is what i tried to attempted.

基本上我有2个相同的MemoryStream,如果说MS1是满的,它写入MS1成文件,然后切换到MS2而MS1是写作,反之亦然。

Basically i have 2 identical memoryStream, if say ms1 is full, it writes ms1 into file and switch to ms2 while ms1 is writing, and vice versa.

    public static class memoryStreamClass
    {
        static MemoryStream ms1 = new MemoryStream();
        static MemoryStream ms2 = new MemoryStream();
        static int c = 1;

        public static void fillBuffer(string outputString)
        {
            byte[] outputByte = Encoding.ASCII.GetBytes(outputString);

            if (c == 1)
            {
                ms1.Write(outputByte, 0, outputByte.Length);

                if (ms1.Length > 8100)
                {
                    c = 2;

                    Thread thread1 = new Thread( () => emptyBuffer(ms1));
                    thread1.Start();

                    ms1.SetLength(0);
                    ms1.Position = 0;
                }
            }
            else
            {
                ms2.Write(outputByte, 0, outputByte.Length);

                if (ms2.Length > 8100)
                {
                    c = 1;

                    Thread thread2 = new Thread(() => emptyBuffer(ms2));
                    thread2.Start();

                    ms2.SetLength(0);
                    ms2.Position = 0;

                }
            }
        }



以上代码可以编译和运行,但是,输出写入并不总是8KB,并将其写入方式过于频繁(比我的单线程程序)。有人可以启发我,并指出有什么不对我的程序?非常感谢你。

the above code can compile and run, however, the output write is not always 8KB, and it writes way too frequently (than my single thread program). can someone enlighten my and points out what is wrong with my program? thank you very much

推荐答案

您的代码是可怕的打破了,你用了两个缓冲区来提高性能的想法几乎肯定过-优化。然而,有一个在这个代码块中的一个明显的问题:

Your code is horribly broken, and your idea of using two buffers to improve performance is almost certainly an over-optimisation. However, there's one obvious problem in this block of code:

Thread thread1 = new Thread( () => emptyBuffer(ms1));
thread1.Start();

ms1.SetLength(0);
ms1.Position = 0;



这段代码的作用是:

What this code does is:


  • 启动一个线程来处理一个缓冲

  • 立即清除缓冲区

问题是,你的清代码的几乎肯定的执行之前,你的线程有机会开始(因为一般来讲,​​线程上下文改变之前执​​行的方法将完成)。所以,你的时候叫 emptyBuffer ,您的MemoryStream 已经是空的。

The problem is that your "clear" code will almost certainly execute before your thread has a chance to start (because generally speaking, an executing method will complete before the thread context changes). So, by the time you call emptyBuffer, your MemoryStream is already empty.

您静是一个坏主意;如果你要传递一个非静态实例的 emptyBuffer 方法,然后设置 MS1 =新的MemoryStream() ,你可能会拥有的更好的功能代码。但最终,这个代码是概念上有缺陷的,你应该看看重新设计。

Your statics are a bad idea; if you were to pass a non-static instance to the emptyBuffer method, and then set ms1 = new MemoryStream(), you would probably have better functioning code. But ultimately, this code is conceptually flawed and you should look at redesigning.

这篇关于写文件需要为交通繁忙第3部分进行了优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆