난 500000 행 conatins 파일을 읽고 있어요.다중 스레드로 단일 파일 읽기 : 속도가 빨라야합니까?
시작 시간 : 1326224619049
완료 시간 : 2 가 나는 결과 다음과 같은 얻을 = 1을 납입에 대한
private void multiThreadRead(int num){
for(int i=1; i<= num; i++) {
new Thread(readIndivColumn(i),""+i).start();
}
}
private Runnable readIndivColumn(final int colNum){
return new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
try {
long startTime = System.currentTimeMillis();
System.out.println("From Thread no:"+colNum+" Start time:"+startTime);
RandomAccessFile raf = new RandomAccessFile("./src/test/test1.csv","r");
String line = "";
//System.out.println("From Thread no:"+colNum);
while((line = raf.readLine()) != null){
//System.out.println(line);
//System.out.println(StatUtils.getCellValue(line, colNum));
}
long elapsedTime = System.currentTimeMillis() - startTime;
String formattedTime = String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes(elapsedTime),
TimeUnit.MILLISECONDS.toSeconds(elapsedTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(elapsedTime))
);
System.out.println("From Thread no:"+colNum+" Finished Time:"+formattedTime);
}
catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("From Thread no:"+colNum +"===>"+e.getMessage());
e.printStackTrace();
}
}
};
}
private void sequentialRead(int num){
try{
long startTime = System.currentTimeMillis();
System.out.println("Start time:"+startTime);
for(int i =0; i < num; i++){
RandomAccessFile raf = new RandomAccessFile("./src/test/test1.csv","r");
String line = "";
while((line = raf.readLine()) != null){
//System.out.println(line);
}
}
long elapsedTime = System.currentTimeMillis() - startTime;
String formattedTime = String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes(elapsedTime),
TimeUnit.MILLISECONDS.toSeconds(elapsedTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(elapsedTime))
);
System.out.println("Finished Time:"+formattedTime);
}
catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
public TesterClass() {
sequentialRead(1);
this.multiThreadRead(1);
}
.... 어떻게 다중 스레드 속도까지 과정을보기 위해 테스트를하고 있습니다 분,
순차 읽기가 종료 14 초 ...........
멀티 스레드 읽기 시작 :
스레드에서 691,363,210번호 : 1 시작 시간 : 스레드에서 1,326,224,753,606
번호 : 1 완료 시간 : 2 분,
멀티 스레드가 종료 읽기 13 초 .....
에 대한 NUM = 5 결과를 얻습니다.
formatted Time:10 min, 20 sec
Sequential read ENDS...........
Multi-Thread read starts:
From Thread no:1 Start time:1326223509574
From Thread no:3 Start time:1326223509574
From Thread no:4 Start time:1326223509574
From Thread no:5 Start time:1326223509574
From Thread no:2 Start time:1326223509574
From Thread no:4 formatted Time:5 min, 54 sec
From Thread no:2 formatted Time:6 min, 0 sec
From Thread no:3 formatted Time:6 min, 7 sec
From Thread no:5 formatted Time:6 min, 23 sec
From Thread no:1 formatted Time:6 min, 23 sec
Multi-Thread read ENDS.....
제 질문은 다음과 같습니다. 다중 스레드 읽기가 약 실행되지 않아야합니다. 2.13 초? 멀티 스레드 솔루션으로 왜 그렇게 오래 걸리는지 설명해 주시겠습니까?
미리 감사드립니다.
[단일 파일을 읽는 Java 다중 스레드 응용 프로그램] 가능한 복제본 (http://stackoverflow.com/questions/8126277/java-multi-thread-application-that-reads-a-single-file) –
스레딩은 다른 디스크에 쓰지 않는 한 작동하지 않습니다.이 경우 두 스레드가 동일한 파일에 쓰려고합니다. 따라서 스레딩은 해당 시나리오에서 작동하지 않습니다. –
@TomaszNurkiewicz - 동일하지 않습니다. 파일 당 하나의 스레드를 사용하고있었습니다. –