简体   繁体   中英

why System.err.println not output in order?

I have a simple java class that uses System.err.println to debug the code as it executes. the purpuse of the class is to find the maximum pairwise product of a given numbers. following is the code and output.

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());
    }
}

the output is (from the output window in Netbeans): 在此输入图像描述

It is clear that the output messages is not in the intended order. It appears to me as if the program execute in multi-thread. what is the error in my code and why it output like this ?

I found the answer here Delay in running thread due to system.out.println statement a stackOverflow member guided me to this.

basically , changing the loge method to the following fixed the issue.

    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);
    }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM