-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultithrededTaskClass.template
64 lines (55 loc) · 1.94 KB
/
MultithrededTaskClass.template
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package %package%;
import %InputClassFQN%;
import %OutputClassFQN%;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
public class %TaskClass% {
private static class CaseSolver implements Callable<CaseSolver> {
void solve() {
output = "";
}
void readInput(%InputClass% in) {
}
void printOutput(%OutputClass% out) {
out.println("Case #" + testNumber + ": " + output);
}
@Override
public CaseSolver call() {
System.err.println("In process testcase #" + testNumber);
solve();
System.err.println("Done testcase #" + testNumber + " (" + output + ")");
return this;
}
CaseSolver(final int testNumber) {
this.testNumber = testNumber;
}
private final int testNumber;
private String output;
}
public void solve(int useless, %InputClass% in, %OutputClass% out) {
final long startTime = System.nanoTime();
ExecutorService pool = Executors.newFixedThreadPool(THREADS_QTY);
int tests = in.nextInt();
List<Future<CaseSolver>> futures = new ArrayList<>(tests);
for (int testCase = 1; testCase <= tests; ++testCase) {
CaseSolver solver = new CaseSolver(testCase);
solver.readInput(in);
futures.add(pool.submit(solver));
}
pool.shutdown();
for (Future<CaseSolver> future : futures) {
CaseSolver solver;
try {
solver = future.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
continue;
}
solver.printOutput(out);
}
final long finishTime = System.nanoTime();
System.err.println("Complete in " + (finishTime - startTime) / 1_000_000_000. + "s");
}
private static final int THREADS_QTY = 4;
}