0

I have a file(0 to N) bytes long, that i want to download and need to write it into file.

What is faster writing into FileChannel with multiple threads or writing into multiple RandomAccessFiles at the same time from each thread that is reading data from URL? Is cost of connecting each part into single file upon completion higher then writing into one FileChannel and be done at the end?

In FileChannels documentation it states

Only one operation that involves the channel's position or can change its file's size may be in progress at any given time; attempts to initiate a second such operation while the first is still in progress will block until the first operation completes.

Thats gives me a hint that writing into multiple files is faster? Or did I understand it wrong?

Does the results change when files are bigger than a few hundred megabytes?

QuakeCore
  • 1,886
  • 2
  • 15
  • 33
Tomas Bisciak
  • 2,801
  • 5
  • 33
  • 57
  • 1
    why do you wanna write it using multiple threads, when downloading a file I wouldn't worry about the time the CPU takes to write on a stream, but my biggest consideration would be the availability of the data on the input stream. – QuakeCore Sep 01 '15 at 14:50
  • is there a download limit on the host of the file ? – QuakeCore Sep 01 '15 at 14:53
  • 1
    there is no way that your application is **not** IO bound already. writing to a shared bottleneck resource like a disk concurrently will just make things exponentially less performant! –  Sep 01 '15 at 15:47
  • 2
    You should be aware that it is unlikely that file operations benefit from multiple threads in general. Hard disks are significantly faster for *sequential* operations (this is also valid, to a lesser extent but still, for SSDs). Processing files with multiple threads causes the read/write operations to *not* happen sequentially, but scattered all over the disc, which most likely will cause the overall performance to degrade. The only justification to use multiple threads would be if you wanted to hide a high latency (from the source that you are reading the data from). – Marco13 Sep 01 '15 at 15:48
  • @Marco13 Will i break "significantly faster for sequential operations" when im writing into different parts of the file?With one thread ? – Tomas Bisciak Oct 04 '15 at 14:02
  • 1
    @TomasBisciak Sure. A bit overly suggestive: Writing 2 bytes at the beginning of a file will be *much* faster than writing 1 byte at the beginning and 1 byte at the end. – Marco13 Oct 04 '15 at 15:02

0 Answers0