Frage

I tried to read to file in same time and return the number of line

ExecutorService executor = Executors.newFixedThreadPool(2);
FutureTask<Integer> futureOne = new FutureTask<Integer>(new Calcul1());
FutureTask<Integer> futureTwo = new FutureTask<Integer>(new Calcul2());
executor.execute(futureOne);
executor.execute(futureTwo);
while (!(futureOne.isDone() && futureTwo.isDone())) {

}

System.out.println(futureOne.get() + futureTwo.get());
executor.shutdown();

That work well but i found is faster if i read file 1 and after file 2... so i don't get any performance boost with futureTash

Why?

War es hilfreich?

Lösung

I suspect that your application is completely IO bound. When you are reading a single file in one thread you would see better performance because you are accessing your files sequentially. When you start two threads, you are reading from two different files which is in effect causing the disk to seek back and forth between the two files. If we are talking about spinning platter type disks, the seek time is a significant portion of the IO.

Andere Tipps

The spin loop wastes one core completely - that's what inter-thread signals are for. If you have just the one disk with two files, there will not be much speedup at all and, in act it may well be negative. If you have files on different disks, (especially networked disks with high latency connections), the there will be a large performance boost.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top