Suppose , i have some x test-cases to be read from input, where each test case parameters are followed by.
How can i create a new thread and run a testcase in it and finally printing the results as the order of test-cases come-by.
Ex:
3
3
1 2 3
2
1 2
10
1 2 3 4 5 6 7 8 9 10
Here first number from System.in gives number of test-case , followed by 3 test-cases, in each test-case first-line contains a number which shows num of input values etc.,
suppose the logic for each is to print the sum, here result would be 6 3 55
This can normally done by sequentially reading input, perform methods, printing.
When operations which cost-time would increase execution-time of program.
So, how can i run each test case in a different and thread, and print results as 6 3 55 would do.shouldn’t print whenever thread ended. I hope my english understandable ……
BTW there’re no instance variables for class.
—This is not for junit or other testing purposes. one program with shaded part as an input giving an output. I’m more like asking for optimizing a program with threads for a case like this
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