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
346 views
in Technique[技术] by (71.8m points)

java - Stack trace is not printed in proper order with other messages on console

Why does it happen that the stack trace printed for the following Java program is not displayed in a proper order on the console screen? It gets mixed up with other messages on the screen.

Is there any parallelism involved which causes it?

Java program:

package evm;

public class Client {

    public static void main(String[] args) {
        EVM evm = new EVM();
        
        try {
            evm.setCandidates(90);   /**An Exception thrown here**/
        } catch (CandidatesOutOfLimitsException e) {
            e.printStackTrace();
            //System.out.print(e.getMessage());
        }
        
        try {
            evm.voteForCandidate(43);    /**An Exception thrown here**/
        } catch (BallotUnitOffException e1) {
            e1.printStackTrace();
            //System.out.print(e1.getMessage());
        }

        evm.pressBallotButton();
        
        System.out.println(evm);  //other messages
        evm.switchOn();
        System.out.println(evm);  //other messages
        
        try {
            evm.voteForCandidate(43);    /**An Exception thrown here**/
        } catch (BallotUnitOffException e) {
            e.printStackTrace();
            //System.out.print(e.getMessage());
        }
    }

}

I have commented against the lines which throw an Exception.

run 1:

evm.CandidatesOutOfLimitsException: Number of Candidates cannot exceed 64
    at evm.EVM.setCandidates(EVM.java:41)
    at evm.Client.main(Client.java:9)
evm.BallotUnitOffException: Ballot Unit is not On
    at evm.BallotUnit.pressCandidateButton(BallotUnit.java:38)
    at evm.EVM.voteForCandidate(EVM.java:59)
    at evm.Client.main(Client.java:16)
evm.BallotUnitOffException: Ballot Unit is not On
    at evm.BallotUnit.pressCandidateButton(BallotUnit.java:38)
    at evm.EVM.voteForCandidate(EVM.java:59)
    at evm.Client.main(Client.java:28)

Control Unit State: evm.Off@42a57993
On Lamp: evm.Off@15db9742Ballot Unit: Ready Lamp: evm.Off@6d06d69c
Slide Switch:evm.SlideSwitchOne@7852e922
Ballot Unit: Ready Lamp: evm.Off@4e25154f
Slide Switch:evm.SlideSwitchTwo@70dea4e
Ballot Unit: Ready Lamp: evm.Off@5c647e05
Slide Switch:evm.SlideSwitchThree@33909752
Ballot Unit: Ready Lamp: evm.Off@55f96302
Slide Switch:evm.SlideSwitchFour@3d4eac69


Control Unit State: evm.On@28d93b30
On Lamp: evm.On@75b84c92Ballot Unit: Ready Lamp: evm.On@6bc7c054
Slide Switch:evm.SlideSwitchOne@7852e922
Ballot Unit: Ready Lamp: evm.On@232204a1
Slide Switch:evm.SlideSwitchTwo@70dea4e
Ballot Unit: Ready Lamp: evm.On@4aa298b7
Slide Switch:evm.SlideSwitchThree@33909752
Ballot Unit: Ready Lamp: evm.On@7d4991ad
Slide Switch:evm.SlideSwitchFour@3d4eac69

run 2:

evm.CandidatesOutOfLimitsException: Number of Candidates cannot exceed 64
    at evm.EVM.setCandidates(EVM.java:41)
    at evm.Client.main(Client.java:9)
evm.BallotUnitOffException: Ballot Unit is not On
    at evm.BallotUnit.pressCandidateButton(BallotUnit.java:38)
    at evm.EVM.voteForCandidate(EVM.java:59)
    at evm.Client.main(Client.java:16)

Control Unit State: evm.Off@42a57993
On Lamp: evm.Off@15db9742Ballot Unit: Ready Lamp: evm.Off@6d06d69c
Slide Switch:evm.SlideSwitchOne@7852e922
Ballot Unit: Ready Lamp: evm.Off@4e25154f
Slide Switch:evm.SlideSwitchTwo@70dea4e
Ballot Unit: Ready Lamp: evm.Off@5c647e05
Slide Switch:evm.SlideSwitchThree@33909752
Ballot Unit: Ready Lamp: evm.Off@55f96302
Slide Switch:evm.SlideSwitchFour@3d4eac69


Control Unit State: evm.On@28d93b30
On Lamp: evm.On@75b84c92Ballot Unit: Ready Lamp: evm.On@6bc7c054
Slide Switch:evm.SlideSwitchOne@7852e922
Ballot Unit: Ready Lamp: evm.On@232204a1
Slide Switch:evm.SlideSwitchTwo@70dea4e
Ballot Unit: Ready Lamp: evm.On@4aa298b7
Slide Switch:evm.SlideSwitchThree@33909752
Ballot Unit: Ready Lamp: evm.On@7d4991ad
Slide Switch:evm.SlideSwitchFour@3d4eac69

evm.BallotUnitOffException: Ballot Unit is not On
    at evm.BallotUnit.pressCandidateButton(BallotUnit.java:38)
    at evm.EVM.voteForCandidate(EVM.java:59)
    at evm.Client.main(Client.java:28)

Similarly I get some other patterns every time I run it. Anybody explain this behavior.

I am using

Eclipse Java EE IDE for Web Developers.

Version: Luna Release (4.4.0)

Build id: 20140612-0600

I apologize for lengthy question

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

This has to do more with the implementation of the standard output and error streams than Eclipse.

Both System.out and System.err are PrintStream objects which may be passed buffered streams that do not automatically flush data to their destinations. Since they are two different streams with buffering used internally, the data you write to them may be interleaved because the streams may decide to flush at different times (the buffer contents may be filled at different times).

Calling System.err.flush() after printing the first two stack traces and then System.out.flush() after printing to the std output stream should get consistent results.


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

...