0

假设,我有一些 x 测试用例要从输入中读取,其中每个测试用例参数都跟在后面。我如何创建一个新线程并在其中运行一个测试用例,最后按照测试用例的顺序打印结果。

Ex:
3 
3
1 2 3
2
1 2
10
1 2 3 4 5 6 7 8 9 10

这里 System.in 中的第一个数字给出了测试用例的数量,然后是 3 个测试用例,在每个测试用例的第一行包含一个显示输入值数量等的数字,假设每个的逻辑是打印总和,这里的结果是 6 3 55

这通常可以通过顺序读取输入、执行方法、打印来完成。当花费时间的操作会增加程序的执行时间时。那么,我怎样才能在不同的线程中运行每个测试用例,并将结果打印为 6 3 55 会做。线程结束时不应该打印。我希望我的英语可以理解......顺便说一句,类没有实例变量。

---这不是用于junit或其他测试目的。一个带有阴影部分的程序作为输入给出输出。对于这样的情况,我更喜欢要求使用线程优化程序

4

2 回答 2

1

This sounds like straight-up ThreadPoolExecutor http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html. You take your input and build up some object that is a Runnable. Submit it to the ThreadPoolExecutor and then just let the output show up on System.out the the Runnable completes.

Or maybe I don't understand your question. I recommend you look into ThreadPoolExecutor and/or Futures.

This guy has good examples: http://www.vogella.de/articles/JavaConcurrency/article.html#threadpools

于 2012-01-06T05:10:41.430 回答
0

如果您使用类似 junit 的工具设置测试用例,那么您可以并行运行测试(junit 对此具有内置支持)。此外,大多数以 java 为中心的构建工具(例如 maven、ant)都支持轻松运行 junit 测试。

于 2012-01-06T04:16:10.187 回答