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?
Multiple Threads reading from the same file(转载)的更多相关文章
- Android 性能优化(16)线程优化:Creating a Manager for Multiple Threads 如何创建一个线程池管理类
Creating a Manager for Multiple Threads 1.You should also read Processes and Threads The previous le ...
- caffe网络在多线程中无法使用GPU的解决方案 | cpp caffe net run in multiple threads
本文首发于个人博客https://kezunlin.me/post/8d877e63/,欢迎阅读! cpp caffe net run in multiple threads Guide set_mo ...
- Apache POI – Reading and Writing Excel file in Java
来源于:https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/ In this article, ...
- [Training Video - 6] [File Reading] Making a Jar file with eclispe, Importing custom jars in SoapUI
Code example : package com.file.properties; import java.io.FileInputStream; import java.util.Propert ...
- [Training Video - 6] [File Reading] [Java] Read Excel File Using Apache POI API
读取以下两种格式的Excel : *.xls and *.xlsx 用Apache POI API来实现,需要用到 HSSF 和 XSSF 的类库 HSSF is the POI Project's ...
- [Training Video - 6] [File Reading] [Java] Read Properties file
package com.file.properties; import java.io.FileInputStream; import java.util.Properties; public cla ...
- 临界区代码 critical section Locks and critical sections in multiple threads
临界区 在同步的程序设计中,临界区段(Critical section)指的是一个访问共享资源(例如:共享设备或是共享存储器)的程序片段,而这些共享资源有无法同时被多个线程访问的特性. 当有线程进入临 ...
- How to: Reading an ini config file from a batch file
Original Link: http://almanachackers.com/blog/2009/12/31/reading-an-ini-config-file-from-a-batch-fil ...
- SQLite multiple threads
const int loops = 1000; public void DatabaseThreadSafetyTest() { var backgroundThread = new Thread(n ...
随机推荐
- 我的Java之旅 第七课 JAVA WEB 会话管理
1.隐藏域 隐藏域其实不是Servlet/JSP的会话管理机制的内容,但它能实现简单的页面状态记录的效果. 2.Cookie Cookie类 setMaxAge() 设置有效期 ...
- python自动化开发-5
列表生成式 生成器 迭代器 列表生成式 举个例子 列表[0, 1, 2, 3, 4, 5, 6],要求把列表里的每个值加1,如何实现呢? L=[0, 1, 2, 3, 4, 5, 6] a={i+ ...
- NoHttp封装--05 文件下载
xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:la ...
- 2.网络编程-udp
# 使用套接字发送udp数据import socket s = socket.socket(socket.AF_INET, SOCK_DGRAM) s.sendto(b"hello" ...
- IP Core 分类
IP(Intelligent Property)核是具有知识产权核的集成电路芯核总称,是经过反复验证过的.具有特定功能的宏模块,与芯片制造工艺无关,可以移植到不同的半导体工艺中.到了SOC阶段,IP核 ...
- 详细理解平衡二叉树AVL与Python实现
前言 上一篇文章讨论的二叉搜索树,其时间复杂度最好的情况下是O(log(n)),但是最坏的情况是O(n),什么时候是O(n)呢? 像这样: 如果先插入10,再插入20,再插入30,再插入40就会成上边 ...
- Go语言学习笔记(一)Let's 干吧
加 Golang学习 QQ群共同学习进步成家立业工作 ^-^ 群号:96933959 简介 Go是Google开发的一种 静态强类型.编译型,并发型,并具有垃圾回收功能的编程语言.为了方便搜索 ...
- zabbix使用自定义脚本监控内存
我这里的脚本是监控centos7系统的内存.centos7系统的内存如何查看我之前的博客都是有的.这里直接写了监控步骤 1.首先是编写脚本. #!/bin/bash mem_total(){ TOTA ...
- BeanFactory和ApplicationContext的简单介绍
引言 Spring通过一个配置文件描述Bean及Bean之间的依赖关系,利用Java语音的反射功能实例化Bean并建立Bean之间的依赖关系.Spring的IoC容器在完成这些底层工作的基础上,还提供 ...
- WPFの获取屏幕分辨率并自适应
double x = SystemParameters.WorkArea.Width;//得到屏幕工作区域宽度 double y = SystemParameters.WorkArea.Height; ...