0

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?

robert trudel
  • 5,283
  • 17
  • 72
  • 124
  • Do you have a spin loop there testing for done-ness? What does that look do? You say "read file 1" but do you mean "call `new Calcul1().run()`"? – Gray Jun 14 '12 at 18:27
  • ya, read file 1 = Calcul1, read fil2 = Calcul2, without thread that take 2 secondes.... with futureTask i get 4.5 seconds – robert trudel Jun 14 '12 at 18:34

2 Answers2

5

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.

Martin James
  • 24,453
  • 3
  • 36
  • 60
2

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.

Gray
  • 115,027
  • 24
  • 293
  • 354