为什么以下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
运行第二步:
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)
每次运行时,我都会得到其他一些模式。有人能解释这种行为吗?
我正在使用:
Eclipse Java EE IDE for Web Developers.
版本:Luna Release(4.4.0)
构建ID:20140612-0600
抱歉问题很长。
println()
是线程安全的,但是printStackTrace()
和println()
的组合可能会导致像这样的结果,因为其中一个方法写入标准错误流,而另一个方法则写入标准输出流。尽管输出被分别记录,但仍可能交错显示在您的控制台上。 - TheLostMinde.printStacktrace
打印到 System.err。 - schtever