堆栈跟踪未按正确顺序与控制台上的其他消息一起打印 [英] Stack trace is not printed in proper order with other messages on console

查看:32
本文介绍了堆栈跟踪未按正确顺序与控制台上的其他消息一起打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 程序:

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.

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

同样,我每次运行它时都会得到一些其他模式.任何人都可以解释这种行为.

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

我正在使用

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

Eclipse Java EE IDE for Web Developers.

版本:Luna 版本 (4.4.0)

Version: Luna Release (4.4.0)

版本号:20140612-0600

Build id: 20140612-0600

对于冗长的问题,我深表歉意

推荐答案

与 Eclipse 相比,这与标准输出和错误流的实现有关.

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

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

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).

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

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.

这篇关于堆栈跟踪未按正确顺序与控制台上的其他消息一起打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆