简体   繁体   English

使用多个线程在单个文件中写入

[英]Using Multiple Threads to write in single file

Can i use multiple threads to write in RandomAccessFile in Java ? 我可以使用多个线程在Java中的RandomAccessFile中编写吗?

I know RandomAccessFiles allows to read & write at any position. 我知道RandomAccessFiles允许在任何位置读写。

I want to create n portions of above File and let each thread to write contents in a particular portion. 我想创建上面文件的n部分,让每个线程在特定部分写入内容。

will it improve IO performance? 会改善IO性能吗?

Eager to hear soon...... 渴望尽快听到……

You can open the file twice with the proper sharing specified, having two RandomAccessFile objects pointing to the same file. 您可以使用指定的正确共享打开文件两次,使两个RandomAccessFile对象指向同一文件。 The OS will manage properly if you're careful not to write and read the same location twice (the OS will handle it then, too, but you'll get unexpected results). 如果小心不要重复读写同一位置两次,则操作系统将正常管理(操作系统也将处理该位置,但会得到意想不到的结果)。

However, it will not improve your I/O performance - CPU is almost never the bottleneck when it comes to I/O. 但是,它不会提高您的I / O性能-CPU几乎从来都不是I / O的瓶颈。 What is it you're trying to achieve? 你想要达到的目标是什么?

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

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