In multi threaded java program, I initiate four concurrent threads with below details

1.  Thread 1 writing file F1 
2.  Thread 2 writing file F2
3.  Thread 3 reading file F3
4.  Thread 4 reading file F4

Its an octa core processor, My question can four threads parallely read or write on hard disk or it will be sequential internally as they are all competing for single resource i.e. hard disk ?

  • 1
    You are not using an SSD in 2018? Aug 12, 2018 at 20:05
  • @MartinSchröder - So, how would the same scenario look like for an SSD ? Jul 31, 2021 at 12:25

2 Answers 2


Assuming all are to the same disk, all read and writes will ultimately be performed one at a time. Assuming each file takes about the same amount of time by itself, your job overall will take about 4x no matter how many cores you have.

They won't necessarily be done sequentially though, and all 4 may appear to be running, but the hard drive will only be doing one read/write at a time. The exact order the operations are done will be seemingly random, subject to the whims of the scheduler and the clock.

For that reason, it may be wise to setup a system yourself to queue up required IO jobs, to ensure yourself they will be done sequentially. You may squeeze a little bit of performance by avoiding the hard disk thrashing about servicing the different requests.

  • They won't necessarily be done sequentially though, and all 4 may appear to be running, but the hard drive will only be doing on read/write at a time. I believe you mean(figures are just for example) , for 15 ms Task1 will be performed, for another 15 ms, another task will be performed and so on.. So it won't be like one task completed end to end then second task is taken up. Instead CPU will devote some cycles to each task (instead of waiting to complete to one task end to end)but one at a time . Right ? Aug 12, 2018 at 6:45
  • @user3198603: That is essentially right, although it is the HDD controller that has to divide its time over the IO tasks and not so much the CPU. Aug 12, 2018 at 6:51
  • @BartvanIngenSchenau Thanks I understand here contesting resource is hard disk. But still during reading or writing CPU will be under work in some way or it will only be HDD controller not CPU right ? Aug 12, 2018 at 7:27
  • 1
    @user3198603: on a modern PC, the HDD controller can access the memory that is needed to read from/write to the harddisk independent of the CPU. The CPU just tells the HDD controller to read/write something and will then focus on other threads/priograms until the HDD controller reports back that it is done. Aug 12, 2018 at 10:16

It is hard to tell what will ultimately happen at the drive level. In between your Java program and the physical head(s) of the hard drive there will be multiple stages of buffering and scheduling trying to make the write process more efficient.

First, the OS file system may save up your output asynchronously and send it on to the device driver at a convenient time. This is known as write-behind caching. It may have a policy, not writing anything for a long time while no one wants to read it. This may improve performance because sequentially writing a lot of data to the same file is typically faster than writing small pieces of different files in an alternating manner.

Since a hard drive is essentially a block device, the driver software may stack up your output and wait for more. Once it has a couple of full blocks stacked up or after it has been a while, it may decide to bother the device with new blocks to be written.

Then the hard disk device itself will have a file system of its own on a lower level (it does not know about files but it knows where the blocks are on the disk and how expensive it will be to write a particular block, given where the heads are at the moment). So it will stack up the blocks it receives from the driver and figure out a smart sequence to write them to the disk, preventing unnecessary head movement as much as possible.

So there is little left for you to worry about and you have virtually no control over the order things will end up on disk if you are writing very quickly, no matter what order you choose at the application level. If write performance matters to your application, the best you can do is try some different scenarios and see how they fare in your system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.