Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

Java with NetBeans 7.2.1 - Execution order issue

Consider the following two classes in a NetBeans Java application. The main class:

public class ExcecutionOrder {
    public static void main(String[] args) {

        Worker worker = new Worker();

        worker.init();
        worker.doProcessing();
        worker.stop();
    }
}

And a worker class like this:

public class Worker {

    public void init() {
        System.out.println("
-------------------------------------------------");
        System.out.println("Worker initialized.");
        System.out.println("-------------------------------------------------
");
    }

    public void doProcessing() {
        printNTimes(2000);
    }

    public void doProcess(int n) {
        printNTimes(n);
    }

    public void printNTimes(int n) {
        System.out.println();
        for (int i = 0; i < n; i++) {
            System.err.println("Output by Worker: " + i);
        }
        System.out.println();
    }

    public void stop() {

        System.out.println("
-------------------------------------------------");
        System.out.println("Worker stopped.");
        System.out.println("-------------------------------------------------
");
    }
}

Strangely, the ouput results in the following order:

Output from Worker: 0
-------------------------------------------------
Output from Worker: 1
Worker initialized.
Output from Worker: 2
-------------------------------------------------
Output from Worker: 3
[...]

Where it should be:

-------------------------------------------------
Worker initialized.
-------------------------------------------------
Output from Worker: 0
Output from Worker: 1
Output from Worker: 3
[...]

If I set the processor affinity of netbeans to use only one cpu core, then at least the initial part is fine and the other controll message (Worker stopped.) is still fragmented resp. interfered by output messages.

Doing the same thing using Eclipse results in the expected execution order.

Has anyone an idea what is happening here? - Many thanks for any advice in advance!

PS: NetBeans 7.2.1 is using jdk1.7.0_03, Eclipse version is Mars.2 Release (4.5.2) - The issue even appears when shifting the code from the worker class to the main method of the main class without using other methods than the main method at all.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are using System.out for your Worker initialized and System.err for your Output by Worker...

System.out and System.err are different output streams, they are flushed at different times, therefore the output is out of order on console.

Use System.out.println() for all your output lines, all outputs would be in the expected order.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...