多线程可以同时将数据写入文件吗? [英] Can multiple threads write data into a file at the same time?

查看:1710
本文介绍了多线程可以同时将数据写入文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你曾经使用过p2p下载软件,他们可以下载一个多线程的文件,他们只创建一个文件,所以我不知道如何线程写入数据到该文件。顺序还是并行?



想象一下,你想要将一个大数据库表转储到一个文件,以及如何使这个工作更快?

解决方案

您可以使用多个线程写入文件,例如日志文件。但你必须协调你的线程作为@Thilo指出。您需要同步文件访问并且只写入整个记录/行,或者您需要有一个策略用于将文件的区域分配给不同的线程。重建具有已知偏移和大小的文件。



这是很少做的性能原因,因为大多数磁盘子系统在顺序写入时性能最好,磁盘IO是瓶颈。如果CPU创建记录或文本行(或网络IO)是它可以帮助的瓶颈。


您要转储的映像一个大数据库表到一个文件,以及如何使这个工作更快?


顺序写入可能是最快的。 / p>

If you have ever used a p2p downloading software, they can download a file with multi-threading, and they created only one file, So I wonder how the threads write data into that file. Sequentially or in parallel?

Imagine that you want to dump a big database table to a file, and how to make this job faster?

解决方案

You can use multiple threads writing a to a file e.g. a log file. but you have to co-ordinate your threads as @Thilo points out. Either you need to synchronize file access and only write whole record/lines, or you need to have a strategy for allocating regions of the file to different threads e.g. re-building a file with known offsets and sizes.

This is rarely done for performance reasons as most disk subsystems perform best when being written to sequentially and disk IO is the bottleneck. If CPU to create the record or line of text (or network IO) is the bottleneck it can help.

Image that you want to dump a big database table to a file, and how to make this job faster?

Writing it sequentially is likely to be the fastest.

这篇关于多线程可以同时将数据写入文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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