I was asked this basic question in an interview that I could not answer:
How can we split a large file between multiple threads to process it? If we run a multi-threaded application, and the input is a large file, we must provide each stream with a part of the file to make it fast. How can we achieve this in Java?
Can someone explain to me how we will do this? Any example would also be appreciated.
Get 3 starting positions
File f = new File("xxx"); long size = f.length(); long p1 = 0; long p2 = f.length() / 3; long p3 = p2 + f.length() / 3;
Move read position and length to stream, move file to position and read n bytes
FileInputStream is = new FileInputStream("xxx"); FileChannel c= is.getChannel(); c.position(position); // read ...
, -. , 1 , , , Vector, n , .
, , : Hyper-threading, deadlocks, busy waiting.
, Java Programming Interviews.