Haz*_*Bot 7 java multithreading bufferedreader java.util.scanner
对于在学校的作业我被要求创建一个简单的程序,创建1000个文本文件,每个文件具有随机数量的行,通过多线程\单个进程计算有多少行.而不是删除那些文件.
现在测试过程中发生了一件奇怪的事情 - 所有文件的线性计数总是比以多线程方式计算它们要快一点,这已经激发了我课堂圈内的学术理论化课程.
当Scanner
用于读取所有文件时,一切都按预期工作 - 在500毫秒线性时间和400毫秒线程时间读取1000个文件
然而,当我使用BufferedReader
时间下降到大约110ms线性和130ms线程.
哪部分代码会导致这个瓶颈?为什么?
编辑:只是为了澄清,我不是问为什么Scanner
工作慢于BufferedReader
.
完整的可编译代码:(虽然你应该改变文件创建路径输出)
import java.io.*;
import java.util.Random;
import java.util.Scanner;
/**
* Builds text files with random amount of lines and counts them with
* one process or multi-threading.
* @author Hazir
*/// CLASS MATALA_4A START:
public class Matala_4A {
/* Finals: */
private static final String MSG = "Hello World";
/* Privates: */
private static int count;
private static Random rand;
/* Private Methods: */ /**
* Increases the random generator.
* @return The new random value.
*/
private static synchronized int getRand() {
return rand.nextInt(1000);
}
/**
* Increments the lines-read counter by a value.
* @param val The amount to be incremented by.
*/
private static synchronized void incrementCount(int val) {
count+=val;
}
/**
* Sets lines-read counter to 0 and Initializes random generator
* by the seed - 123.
*/
private static void Initialize() {
count=0;
rand = new Random(123);
}
/* Public Methods: */ /**
* Creates n files with random amount of lines.
* @param n The amount of files to be created.
* @return String array with all the file paths.
*/
public static String[] createFiles(int n) {
String[] array = new String[n];
for (int i=0; i<n; i++) {
array[i] = String.format("C:\\Files\\File_%d.txt", i+1);
try ( // Try with Resources:
FileWriter fw = new FileWriter(array[i]);
PrintWriter pw = new PrintWriter(fw);
) {
int numLines = getRand();
for (int j=0; j<numLines; j++) pw.println(MSG);
} catch (IOException ex) {
System.err.println(String.format("Failed Writing to file: %s",
array[i]));
}
}
return array;
}
/**
* Deletes all the files who's file paths are specified
* in the fileNames array.
* @param fileNames The files to be deleted.
*/
public static void deleteFiles(String[] fileNames) {
for (String fileName : fileNames) {
File file = new File(fileName);
if (file.exists()) {
file.delete();
}
}
}
/**
* Creates numFiles amount of files.<br>
* Counts how many lines are in all the files via Multi-threading.<br>
* Deletes all the files when finished.
* @param numFiles The amount of files to be created.
*/
public static void countLinesThread(int numFiles) {
Initialize();
/* Create Files */
String[] fileNames = createFiles(numFiles);
Thread[] running = new Thread[numFiles];
int k=0;
long start = System.currentTimeMillis();
/* Start all threads */
for (String fileName : fileNames) {
LineCounter thread = new LineCounter(fileName);
running[k++] = thread;
thread.start();
}
/* Join all threads */
for (Thread thread : running) {
try {
thread.join();
} catch (InterruptedException e) {
// Shouldn't happen.
}
}
long end = System.currentTimeMillis();
System.out.println(String.format("threads time = %d ms, lines = %d",
end-start,count));
/* Delete all files */
deleteFiles(fileNames);
}
@SuppressWarnings("CallToThreadRun")
/**
* Creates numFiles amount of files.<br>
* Counts how many lines are in all the files in one process.<br>
* Deletes all the files when finished.
* @param numFiles The amount of files to be created.
*/
public static void countLinesOneProcess(int numFiles) {
Initialize();
/* Create Files */
String[] fileNames = createFiles(numFiles);
/* Iterate Files*/
long start = System.currentTimeMillis();
LineCounter thread;
for (String fileName : fileNames) {
thread = new LineCounter(fileName);
thread.run(); // same process
}
long end = System.currentTimeMillis();
System.out.println(String.format("linear time = %d ms, lines = %d",
end-start,count));
/* Delete all files */
deleteFiles(fileNames);
}
public static void main(String[] args) {
int num = 1000;
countLinesThread(num);
countLinesOneProcess(num);
}
/**
* Auxiliary class designed to count the amount of lines in a text file.
*/// NESTED CLASS LINECOUNTER START:
private static class LineCounter extends Thread {
/* Privates: */
private String fileName;
/* Constructor: */
private LineCounter(String fileName) {
this.fileName=fileName;
}
/* Methods: */
/**
* Reads a file and counts the amount of lines it has.
*/ @Override
public void run() {
int count=0;
try ( // Try with Resources:
FileReader fr = new FileReader(fileName);
//Scanner sc = new Scanner(fr);
BufferedReader br = new BufferedReader(fr);
) {
String str;
for (str=br.readLine(); str!=null; str=br.readLine()) count++;
//for (; sc.hasNext(); sc.nextLine()) count++;
incrementCount(count);
} catch (IOException e) {
System.err.println(String.format("Failed Reading from file: %s",
fileName));
}
}
} // NESTED CLASS LINECOUNTER END;
} // CLASS MATALA_4A END;
Run Code Online (Sandbox Code Playgroud)
Dav*_*INO 10
瓶颈是磁盘.
您每次只能使用一个线程访问磁盘,因此使用多个线程无济于事,而线程切换所需的加班时间将减慢您的全局性能.
只有当你需要拆分你的工作等待在不同的源(例如网络和磁盘,或两个不同的磁盘,或许多网络流)上进行长I/O操作,或者如果你有一个cpu密集型操作可以使用多线程时,使用多线程是很有趣的.在不同的核心之间分裂.
请记住,对于一个好的多线程程序,您需要始终考虑:
可能有不同的因素:
最重要的是避免同时从多个线程访问磁盘(但是因为你在SSD上,你可能会侥幸逃脱).但是,在普通的硬盘上,从一个文件切换到另一个文件可能需要10毫秒的搜索时间(取决于数据的缓存方式).
1000个线程太多,尝试使用核心数*2.太多时间只会丢失切换上下文.
尝试使用线程池.总时间在110毫秒到130毫秒之间,其中一部分来自创建线程.
一般来说,在测试中做一些更多的工作.定时110ms并不总是那么准确.还取决于当时正在运行的其他进程或线程.
尝试切换测试的顺序,看看它是否有所作为(缓存可能是一个重要因素)
countLinesThread(num);
countLinesOneProcess(num);
Run Code Online (Sandbox Code Playgroud)此外,根据系统,currentTimeMillis()
可能具有10至15毫秒的分辨率.因此,短时间运行并不是非常准确.
long start = System.currentTimeMillis();
long end = System.currentTimeMillis();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1702 次 |
最近记录: |