简体   繁体   English

多线程写入文件的行为

[英]Behaviour in file writing by multiple threads

Many threads are writing StringBuffer to same file. 许多线程将StringBuffer写入同一文件。 StringBuffer contains around 100 lines. StringBuffer包含大约100行。 What is the output in file if multiple threads write to same file. 如果多个线程写入同一文件,文件中的输出是什么。 Is each output from different threads mixup in file or they appear sequentially. 是来自不同线程混合的每个输出在文件中还是顺序出现。

all of the above plus an exception could be thrown on one or more of the threads 以上所有内容以及一个或多个线程上可能引发异常

Without some sort of synchronisation between the threads the result is non deterministic. 如果线程之间没有某种同步,则结果是不确定的。

The data is likely to appear in the order it is written. 数据可能会按照写入顺序出现。 However, unless you control this, that order is likely to be somewhat random. 但是,除非您控制此顺序,否则该顺序可能会有些随机。

Using multi-threads is also likely to be much slower, esp for such a small file. 使用多线程也可能会慢很多,尤其是对于这么小的文件。 Its could be as many times slower as you have threads. 它的速度可能是线程的数倍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM