我希望在eclipse控制台中输出logger和inputstream
。但每次执行时,结果总是不一样。我有几个类,一个主类调用其他类,我把logger放到每个方法进行调试。我将结果打印到控制台。我也有方法将inputstream
检索为string
并将其打印到控制台中。
输出示例
mai 19, 2015 4:10:58 PM ScriptPack.Section findSection
INFO: findSection OK
mai 19, 2015 4:10:58 PM ScriptPack.Section convertStringToTestCase
INFO: convertStringToTestCase OK
mai 19, 2015 4:10:58 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:10:58 PM ScriptPack.TestCase getName
INFO: getName OK
//Error.
//**********EasyTest Test Report**********
//Test Case: testCase/test1.tc
//Result : FAILED
//Start at: 2015-05-19-T16:10:58.479
mai 19, 2015 4:10:58 PM ScriptPack.TestCase settIterationNumber
INFO: settIterationNumber OK
mai 19, 2015 4:10:58 PM ScriptPack.TestCase getIterationNumber
INFO: getIterationNumber OK
mai 19, 2015 4:10:58 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
Iteration number : 1
mai 19, 2015 4:10:58 PM ScriptPack.TestCase getName
INFO: getName OK
//**********EasyTest Test Report**********
//Test Case: testCase/test2.tc
mai 19, 2015 4:10:59 PM ScriptPack.TestCase setIsPassed
INFO: setIsPassed OK
//Result : PASSED
//Start at: 2015-05-19-T16:10:58.838
Iteration number : 1
mai 19, 2015 4:10:59 PM ScriptPack.TestCase settIterationNumber
INFO: settIterationNumber OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIterationNumber
INFO: getIterationNumber OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getName
INFO: getName OK
//**********EasyTest Test Report**********
//Test Case: testCase/test3.tc
mai 19, 2015 4:10:59 PM ScriptPack.TestCase setIsPassed
INFO: setIsPassed OK
//Result : PASSED
//Start at: 2015-05-19-T16:10:59.229
mai 19, 2015 4:10:59 PM ScriptPack.TestCase settIterationNumber
INFO: settIterationNumber OK
Iteration number : 1
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIterationNumber
INFO: getIterationNumber OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCaseList verifyList
INFO: verifyList NOK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getName
INFO: getName OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase settIterationNumber
INFO: settIterationNumber OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIterationNumber
INFO: getIterationNumber OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:10:59 PM ScriptPack.TestCaseList verifyList
INFO: verifyList NOK
//Error.
//**********EasyTest Test Report**********
//Test Case: testCase/test1.tc
//Result : FAILED
//Start at: 2015-05-19-T16:10:59.635
Iteration number : 2
然后我重新执行并获得不同的顺序
mai 19, 2015 4:13:24 PM ScriptPack.Section findSection
INFO: findSection OK
mai 19, 2015 4:13:24 PM ScriptPack.Section convertStringToTestCase
INFO: convertStringToTestCase OK
mai 19, 2015 4:13:24 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:13:24 PM ScriptPack.TestCase getName
INFO: getName OK
//Error.
//**********EasyTest Test Report**********
//Test Case: testCase/test1.tc
//Result : FAILED
//Start at: 2015-05-19-T16:13:24.321
mai 19, 2015 4:13:24 PM ScriptPack.TestCase settIterationNumber
INFO: settIterationNumber OK
mai 19, 2015 4:13:24 PM ScriptPack.TestCase getIterationNumber
INFO: getIterationNumber OK
Iteration number : 1
mai 19, 2015 4:13:24 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:13:24 PM ScriptPack.TestCase getName
INFO: getName OK
//**********EasyTest Test Report**********
//Test Case: testCase/test2.tc
mai 19, 2015 4:13:25 PM ScriptPack.TestCase setIsPassed
INFO: setIsPassed OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase settIterationNumber
INFO: settIterationNumber OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIterationNumber
INFO: getIterationNumber OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getName
INFO: getName OK
//Result : PASSED
//Start at: 2015-05-19-T16:13:24.789
Iteration number : 1
//**********EasyTest Test Report**********
//Test Case: testCase/test3.tc
//Result : PASSED
//Start at: 2015-05-19-T16:13:25.228
mai 19, 2015 4:13:25 PM ScriptPack.TestCase setIsPassed
INFO: setIsPassed OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase settIterationNumber
INFO: settIterationNumber OK
Iteration number : 1
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIterationNumber
INFO: getIterationNumber OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCaseList verifyList
INFO: verifyList NOK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getName
INFO: getName OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase settIterationNumber
INFO: settIterationNumber OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIterationNumber
INFO: getIterationNumber OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
mai 19, 2015 4:13:25 PM ScriptPack.TestCase getIsPassed
INFO: getIsPassed OK
//Error.
//**********EasyTest Test Report**********
//Test Case: testCase/test1.tc
//Result : FAILED
//Start at: 2015-05-19-T16:13:25.587
Iteration number : 2
mai 19, 2015 4:13:25 PM ScriptPack.TestCaseList verifyList
INFO: verifyList NOK
该行以inputstream
的双斜线开头,应该汇编。我试图在每个方法中放置synchronized但不起作用。如果我放Thread.sleep()
,我应该把它放在哪里?有没有更多安全方法来同步输出?
答案 0 :(得分:1)
通过对问题的评论中的讨论解决了该问题。我发布此信息作为答案,以防其他人遇到同样的问题并在搜索时发现此问题。
如果没有进行任何特定配置,Logger API通常会将记录输出到stderr
。拨打System.out.println()
的电话当然会转到stdout
。
在混合两种方法时,每次运行程序时输出到控制台的问题都不同,这是因为stdout
通常是缓冲的,而stderr
则不是。stdout
。要解决此类问题,必须确保两种方法都打印到相同类型的输出流,包括stderr
或两者都打印到System.out.println()
。
要了解如何为记录器API执行此操作,您必须查看记录器的文档。对于System.err
(以及其他打印方法),您必须改为System.err.println()
,即modfit <- train(as.factor(target)~., method="rf", data=new)
,反之亦然。
答案 1 :(得分:0)
而不是睡眠,如果您知道每个线程需要等待的时间,则可以使用wait。 或者使用一个线程来锁定并打开另一个......这一切都取决于您的需求。 使用多线程时,除非它们不同时工作,否则结果将永远不会完全相同。 如果订单无效,则需要进行一些更改。 尝试使用其中一个线程池系列对象。 这里有一个可能有帮助的教程 http://www.tutorialspoint.com/java/java_multithreading.htm