zoukankan      html  css  js  c++  java
  • Multiple Threads reading from the same file(转载)


    I have a xml file that needs to be read from many many times. I am trying to use the Parallel.ForEach to speed this processes up since none of that data being read in is relevant as to what order it is being read in. The data is just being used to populate objects. My problem is even though I am opening the file each time in the thread as read only it complains that it is open by another program. (I don't have it opened in a text editor or anything :))
    How can I accomplish multi reads from the same file?
    EDIT: The file is ~18KB pretty small. It is read from about 1,800 times.
    Thanks


    If you want multiple threads to read from the same file, you need to specify FileShare.Read:

    using (var stream = File.Open("theFile.xml", FileMode.Open, FileAccess.Read, FileShare.Read))
    {
        ...
    }

    However, you will not achieve any speedup from this, for multiple reasons:

    • Your hard disk can only read one thing at a time. Although you have multiple threads running at the same time, these threads will all end up waiting for each other.
    • You cannot easily parse a part of an XML file. You will usually have to parse the entire XML file every time. Since you have multiple threads reading it all the time, it seems that you are not expecting the file to change. If that is the case, then why do you need to read it multiple times?

    原文链接

  • 相关阅读:
    Oracle安装
    自动化测试开发
    Hyperf 接入阿里云ACM应用配置管理中心
    PHP redis有序集合实现分页
    虚拟现实与大数据
    树形数据结构化
    立即执行函数
    jQuery中的显示与隐藏
    jQuery中的HTML
    Django+Nginx配置+前后端交互
  • 原文地址:https://www.cnblogs.com/OpenCoder/p/10336402.html
Copyright © 2011-2022 走看看