堆栈跟踪不会以正确的顺序打印到控制台上的其他消息

时间:2015-07-19 13:39:02

标签: java eclipse console stack-trace

为什么是否会在控制台屏幕上以正确的顺序显示为以下Java程序打印的堆栈跟踪?它与屏幕上的其他消息混在一起。

是否存在导致它的任何并行性?

Java程序:

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

}

我评论了抛出异常的行。

运行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

运行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)

同样,每次运行时我都会得到一些其他模式。有人解释这种行为。

我正在使用

  

面向Web开发人员的Eclipse Java EE IDE。

     

版本:Luna Release(4.4.0)

     

构建ID:20140612-0600

我为冗长的问题道歉

1 个答案:

答案 0 :(得分:5)

与Eclipse相比,标准输出和错误流的实现需要做的更多。

System.outSystem.err都是PrintStream个对象,可以传递缓冲流,这些流不会自动将数据刷新到目标。由于它们是两个内部使用缓冲的不同流,因此您写入它们的数据可能是交错的,因为流可能决定在不同时间刷新(缓冲区内容可能在不同时间填充)。

打印前两个堆栈跟踪后调用System.err.flush(),然后在打印到std输出流后调用System.out.flush()应得到一致的结果。