为什么启动后我的Java程序性能会显着下降?

Bri*_*ter 5 java concurrency performance multithreading

我正在编写一个小型Java应用程序来分析大量的图像文件.现在,它通过平均图像中每个像素的亮度并将其与文件夹中的其他图像进行比较,找到文件夹中最亮的图像.

有时,我在启动后立即获得100+图像/秒的速率,但这几乎总是下降到<20图像/秒,我不知道为什么.当它处于100+图像/秒时,CPU使用率为100%,但随后降至20%左右,这似乎太低了.

这是主要课程:

public class ImageAnalysis {

    public static final ConcurrentLinkedQueue<File> queue = new ConcurrentLinkedQueue<>();
    private static final ConcurrentLinkedQueue<ImageResult> results = new ConcurrentLinkedQueue<>();
    private static int size;
    private static AtomicInteger running = new AtomicInteger();
    private static AtomicInteger completed = new AtomicInteger();
    private static long lastPrint = 0;
    private static int completedAtLastPrint;

    public static void main(String[] args){
        File rio = new File(IO.CAPTURES_DIRECTORY.getAbsolutePath() + File.separator + "Rio de Janeiro");

        String month = "12";

        Collections.addAll(queue, rio.listFiles((dir, name) -> {
            return (name.substring(0, 2).equals(month));
        }));

        size = queue.size();

        ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1);

        for (int i = 0; i < 8; i++){
            AnalysisThread t = new AnalysisThread();
            t.setPriority(Thread.MAX_PRIORITY);
            executor.execute(t);
            running.incrementAndGet();
        }
    }

    public synchronized static void finished(){
        if (running.decrementAndGet() <= 0){

            ImageResult max = new ImageResult(null, 0);

            for (ImageResult r : results){
                if (r.averageBrightness > max.averageBrightness){
                    max = r;
                }
            }

            System.out.println("Max Red: " + max.averageBrightness + " File: " + max.file.getAbsolutePath());
        }
    }

    public synchronized static void finishedImage(ImageResult result){
        results.add(result);
        int c = completed.incrementAndGet();

        if (System.currentTimeMillis() - lastPrint > 10000){
            System.out.println("Completed: " + c + " / " + size + " = " + ((double) c / (double) size) * 100 + "%");
            System.out.println("Rate: " + ((double) c - (double) completedAtLastPrint) / 10D + " images / sec");
            completedAtLastPrint = c;

            lastPrint = System.currentTimeMillis();
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

而线程类:

public class AnalysisThread extends Thread {

    @Override
    public void run() {
        while(!ImageAnalysis.queue.isEmpty()) {
            File f = ImageAnalysis.queue.poll();

            BufferedImage image;
            try {
                image = ImageIO.read(f);

                double color = 0;
                for (int x = 0; x < image.getWidth(); x++) {
                    for (int y = 0; y < image.getHeight(); y++) {
                        //Color c = new Color(image.getRGB(x, y));
                        color += image.getRGB(x,y);
                    }
                }

                color /= (image.getWidth() * image.getHeight());

                ImageAnalysis.finishedImage((new ImageResult(f, color)));

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        ImageAnalysis.finished();
    }
}
Run Code Online (Sandbox Code Playgroud)

Pet*_*rey 8

您似乎混淆了使用线程池和创建自己的线程.我建议你使用或其他.实际上我建议你只使用固定的线程池

最有可能发生的事情是你的线程正在获得一个正在丢失但却杀死杀死线程的任务的异常.

我建议你只是线程池,不要尝试创建自己的线程,或者排队,因为这是ExecutorService为你做的.对于每个任务,将其提交到池中,每个图像一个,如果您不打算检查任何任务的错误,我建议您捕获所有Throwable并记录它们,否则您可能会得到一个RuntimeExcepion或者Error不知道这发生了.

如果你有Java 8,更简单的方法是使用parallelStream().您可以使用它来同时分析图像并收集结果,而无需分割工作并收集结果.例如

List<ImageResults> results = Stream.of(rio.listFiles())
                                   .parallel()
                                   .filter(f -> checkFile(f))
                                   .map(f -> getResultsFor(f))
                                   .list(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)