It's not a secret anymore!
The moose likes Threads and Synchronization and the fly likes Concurrent file I/O Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Concurrent file I/O" Watch "Concurrent file I/O" New topic

Concurrent file I/O

Don Redd
Ranch Hand

Joined: Jan 05, 2012
Posts: 82

Hi ,
I have a static function which reads a file and splits it (I am using BufferedReader/Printwriters). and I am invoking function from run method.
1)Now am starting a thread to split a file(i.e A.txt to A.txt0 and A.txt1) and Lets say it took 100 seconds on average.

Here comes the real problem

2)Now , I am starting two threads to split two different Files in parallel/Concurrently, but now I see the same A.txt is taking more than 100 secs (150+ on average) of time.

Is there anything that I am missing while mixing up IO with Concurrency?

Don Redd
Ranch Hand

Joined: Jan 05, 2012
Posts: 82

I posted a repeat by accident,Moderators please delete this post , Thanks
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

I removed the duplicate.

The problem you have is typical fore doing IO in two threads. If you are writing to a single disk, there is typically just one write head on the disk. The two threads will be writing to different disk locations. One of two things is likely to happen, one thread monopolizes the write head causing the other thread to wait, or both threads take slices of write head time. In the second scenario the write head had to move back and forth between two different locations so it spends more time seeking and writing both files concurrently takes more time than writing them in series.

So it is often better to use a single thread for file writing, unless you have special circumstances like disks with multiple write heads, a multi disk RAID, writing the files to different disks, or maybe using SSDs.

Don Redd
Ranch Hand

Joined: Jan 05, 2012
Posts: 82

Thanks Steve
I agree. Here's the link:
subject: Concurrent file I/O
jQuery in Action, 3rd edition