0

同時にファイルを読み込んで行数を返そうとしました

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();

それはうまく機能しますが、ファイル1とファイル2の後に読んだ方が速いことがわかりました...したがって、futureTashでパフォーマンスが向上することはありません

なんで?

4

2 に答える 2

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.

于 2012-06-14T18:42:33.993 に答える
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.

于 2012-06-14T18:42:54.563 に答える