• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Concurrent file I/O

 
Don Redd
Ranch Hand
Posts: 82
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thanks
Don..
 
Don Redd
Ranch Hand
Posts: 82
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I posted a repeat by accident,Moderators please delete this post , Thanks
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic