为什么System.err.println无法按顺序输出?

时间:2019-03-10 18:45:54

标签: java multithreading netbeans

我有一个简单的Java类,它使用System.err.println在执行代码时对其进行调试。该类的目的是找到给定数字的最大成对乘积。 以下是代码和输出。

public class MaxPairwiseProduct {
private static boolean enableLog = true;

static long getMaxPairwiseProductFast(int[] numbers) {
    long max_product = 0;
    int n = numbers.length;
    int firstMaxInt = -1;
    int secondMaxInt = -1;

    int firstMaxIndex = 0;
    int secondMaxIndex = 0;


    loge("firstMax initialized :" + firstMaxInt);
    loge("secondMax initialized :"+ secondMaxInt);
    loge("***********************************************");

    for (int firstPassIndex = 1; firstPassIndex < n; firstPassIndex++) {
        loge("firstpass : Number " +firstPassIndex);
        if (numbers[firstPassIndex] > firstMaxInt  ) 
        {
            loge("\t firstpass : Found max  " +numbers[firstPassIndex]);
            firstMaxInt = numbers[firstPassIndex] ;
            firstMaxIndex = firstPassIndex ;
        }
    }

    for (int secondPassIndex = 1; secondPassIndex < n; secondPassIndex++) {
        loge("secondPassIndex : Number " +numbers[secondPassIndex]);
        if (numbers[secondPassIndex] > secondMaxInt  && secondPassIndex != firstMaxIndex ) 
        {
            loge("\t firstpass : Found max  " +secondPassIndex);
            secondMaxInt = numbers[secondPassIndex] ;
            secondMaxIndex = secondPassIndex;
        }
    }
    max_product = firstMaxInt * secondMaxInt ;
    return max_product;

}

public static void main(String[] args) {
    FastScanner scanner = new FastScanner(System.in);
    int n = scanner.nextInt();
    int[] numbers = new int[n];
    for (int i = 0; i < n; i++) {
        numbers[i] = scanner.nextInt();
    }
    System.out.println(getMaxPairwiseProductFast(numbers));
}
private static void loge(String s)
{
if (enableLog == true)
    {
        System.err.println(s);
    }
}
private static void log(String s)
{
    if (enableLog == true)
    {
        System.out.println(s);
    }

}
static class FastScanner {
    BufferedReader br;
    StringTokenizer st;

    FastScanner(InputStream stream) {
        try {
            br = new BufferedReader(new
                InputStreamReader(stream));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    String next() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }
}

输出为(从Netbeans的输出窗口中): enter image description here

很明显,输出消息的顺序不正确。 在我看来,程序似乎在多线程中执行。 我的代码中有什么错误,为什么会这样输出?

1 个答案:

答案 0 :(得分:0)

我在这里找到了答案 Delay in running thread due to system.out.println statement 一个stackOverflow成员将我引向了这一点。

基本上,将loge方法更改为以下内容可以解决此问题。

    private static void loge(String s)
{
if (enableLog == true)
    {
        System.err.println(s);
    try {
        Thread.sleep(100);
    } catch (InterruptedException ex) {
        Logger.getLogger(MaxPairwiseProduct.class.getName()).log(Level.SEVERE, null, ex);
    }
    }
}