マルチスレッドIO密集型テスト
3078 ワード
/**
*
*
*/
@SuppressWarnings("unchecked")
public static void main(String[] args){
try{
//
int poolSize = 20;
long startIndex = 0;//
int batchSize = 1024*1024*3;// ,
long total =new File("E:/Tool/CentOS.5.6.iso").length();
System.out.println(total);
//
ExecutorService executorService = Executors.newFixedThreadPool(poolSize);
LinkedList<Future> queue = new LinkedList<Future>();
long start =System.currentTimeMillis();
while(startIndex <= total){
FileInputStream fis = new FileInputStream(new File("E:/Tool/CentOS.5.6.iso"));
queue.add(executorService.submit(new ThreadTask(startIndex,batchSize,fis)));
startIndex += batchSize;
}
//
executorService.shutdown();
while(!queue.isEmpty()){
try {
queue.pop().get();
} catch (Exception e) {
System.out.println("+ ");
}
}
long end =System.currentTimeMillis();
System.out.println(" :"+String.valueOf(end-start));
System.out.println(" :"+ThreadTask.bbb.toString());
}catch (Exception e) {
e.printStackTrace();
}
}
}
/**
*
*
*/
class ThreadTask implements Runnable{
private FileInputStream fis;
private long startIndex;
private int batchSize;
public static AtomicLong bbb = new AtomicLong(0);
public ThreadTask(long startIndex,int batchSize,FileInputStream fis){
this.startIndex=startIndex;
this.fis=fis;
this.batchSize=batchSize;
}
@Override
public void run() {
try {
//System.out.println(Thread.currentThread().getName()+" ");
fis.skip(startIndex);
byte[] b = new byte[batchSize];//
int flag = fis.read(b);
if(flag != -1){
bbb.addAndGet(flag);
}
fis.close();
//System.out.println(Thread.currentThread().getName()+" ");
} catch (Exception e) {
e.printStackTrace();
}
}
テスト結果:
パソコン:華碩N 56 V
cpu:intel core i5
ハードディスク:750 GB
シングルスレッド
時間のサイズ
42900 4262373376
40545
43477
41259
40810
40591
マルチスレッド
タイムスレッドサイズ
41855 2 4262373376
41278
42853
42619
45071
43742
43025 3 4262373376
42978
44616
45128
43685
46785
42370 4 4262373376
41371 4
41496
42665
40403
42369
42370 5 4262373376
53539
40373
41215
44678 10 4262373376
43400 10
46768 20 4262373376
48204 20