为什么添加System.out.println可使线程运行更快?

时间:2019-06-16 22:21:28

标签: java multithreading

我正在尝试各种使大型计算并行运行的方法。除明确地创建和启动Thread实例(这要慢得多)之外,它的工作符合预期。我添加了一些println输出,以检查线程是否真正并行运行,但所做的更改使它快得多。速度从“超过一秒”变为“数十毫秒”,结果是可重复的。

非常古怪:ArrayList没有慢度问题,并且println没有明显的区别,但是LinkedList直到我添加一些println为止都很慢。

我在具有Java 8的Windows 10上运行此程序。我还尝试了Java 11和12,并且没有发生速度慢(因此无意义的输出无法使其更快)的情况。

我的问题不是如何使其更快-我实际上不需要运行此代码,而我可以用另一种方式使其并行(我建议使用流)。我的问题是如何通过打印一些输出来加快线程速度?或者,如果您更喜欢为什么速度太慢

我尝试改变线程数:一个线程(!)消失了怪异现象,但出现了2个或更多线程(我测试了我的机器上最多两倍的内核)。

我尝试更改正在处理的列表的大小,但仅证明较小的列表可以更快地处理!

我尝试改变输出:两个println输出似乎将速度提高了一个以上,并且在线程寿命结束时进行的打印效果比开始时要差。

我确保它不是GC效果(巨大的堆,并检查了GC日志)。

这是我正在运行的代码。

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class ListAddThreads {
    private static final int SIZE    = 20000000;
    private static final int REPEATS = 3;
    private static final int THREADS = Runtime.getRuntime().availableProcessors() / 2;

    private static String log = "none";

    public static void main( String[] args ) {
        if( args.length > 0 ) switch( args[0] ) {
            case "some":
            case "more":
            case "lots":
                log = args[0];
        }

        System.out.println( "Array List:" );
        List<Integer>listA = new ArrayList<>(SIZE);
        for( int i = 0; i < SIZE; i++ )
            listA.add( i );
        for( int i = 0; i < REPEATS; i++ )
            add( listA );

        System.out.println( "\nLinked List:" );
        List<Integer>listL = new LinkedList<>(listA);
        for( int i = 0; i < REPEATS; i++ )
            add( listL );
    }

    private static class AdderThread extends Thread {
        private long          result = 0;
        private List<Integer> list;
        private int           from;
        private int           to;

        AdderThread( List<Integer> list, int from, int to ) {
            this.list = list;
            this.from = from;
            this.to   = to;
        }

        long getResult() {
            return result;
        }

        public void run() {
            switch( log ) {
                case "some":
                    System.err.println( "string literal" );
                    break;
                case "more":
                    System.err.println( Thread.currentThread().getName() + " starting" );
                    break;
                case "lots":
                    System.err.println( "string literal" );
                    System.err.println( Thread.currentThread().getName() + " starting" );
                    break;
            }
            Iterator<Integer> it = list.iterator();
            for( int i = 0; i < to; i++ ) {
                Integer j = it.next();
                if( i >= from )
                    result += j;
            }
        }
    }

    private static void add( List<Integer> list ) {
        long          start  = System.currentTimeMillis();
        AdderThread[] adders = new AdderThread[THREADS];
        int           size   = list.size() / THREADS;

        for( int i = 0; i < THREADS; i++ ) {
            int from = i * size;
            int to   = i == THREADS - 1 ? list.size() : (i + 1) * size;

            adders[i] = new AdderThread( list, from, to );
        }
        for( int i = 0; i < THREADS; i++ ) {
            adders[i].start();
        }
        long result = 0;
        try {
            for( int i = 0; i < THREADS; i++ ) {
                adders[i].join();
                result += adders[i].getResult();
            }
        } catch ( InterruptedException x ) {
            throw new RuntimeException( "Adder thread interrupted.", x );
        }
        System.out.println( "Sum: " + result + ", time: " + (System.currentTimeMillis() - start) );
    }
}

我这样称呼它:

D:\weird> for %G in (none some more lots) do java -Xms4G -Xmx4G -XX:+PrintGCDetails -Xloggc:ListAddThreads_%G_gc.log ListAddThreads %G 2>nul

以下是一些示例输出,该示例显示LinkedList的时间随着更多输出(被重定向到遗忘)而变得更快:

java -Xms4G -Xmx4G -XX:+PrintGCDetails -Xloggc:ListAddThreads_none_gc.log ListAddThreads none  2>nul
Array List:
Sum: 199999990000000, time: 62
Sum: 199999990000000, time: 157
Sum: 199999990000000, time: 62

Linked List:
Sum: 199999990000000, time: 3343
Sum: 199999990000000, time: 1814
Sum: 199999990000000, time: 1796

java -Xms4G -Xmx4G -XX:+PrintGCDetails -Xloggc:ListAddThreads_some_gc.log ListAddThreads some  2>nul
Array List:
Sum: 199999990000000, time: 50
Sum: 199999990000000, time: 58
Sum: 199999990000000, time: 56

Linked List:
Sum: 199999990000000, time: 2777
Sum: 199999990000000, time: 560
Sum: 199999990000000, time: 527

java -Xms4G -Xmx4G -XX:+PrintGCDetails -Xloggc:ListAddThreads_more_gc.log ListAddThreads more  2>nul
Array List:
Sum: 199999990000000, time: 48
Sum: 199999990000000, time: 56
Sum: 199999990000000, time: 39

Linked List:
Sum: 199999990000000, time: 108
Sum: 199999990000000, time: 93
Sum: 199999990000000, time: 94

java -Xms4G -Xmx4G -XX:+PrintGCDetails -Xloggc:ListAddThreads_lots_gc.log ListAddThreads lots  2>nul
Array List:
Sum: 199999990000000, time: 52
Sum: 199999990000000, time: 68
Sum: 199999990000000, time: 39

Linked List:
Sum: 199999990000000, time: 115
Sum: 199999990000000, time: 89
Sum: 199999990000000, time: 91

0 个答案:

没有答案