堆栈跟踪与控制台上的其他消息未按正确顺序打印 [英] Stack trace is not printed in proper order with other messages on console
问题描述
为什么为什么为以下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());
}
}
}
我对这句话有意见
运行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.out
和 System.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()
,然后调用 System.out.flush()
打印到std输出流后应该得到一致的结果。
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屋!