Skip to content

Commit

Permalink
improved implementation using Executors
Browse files Browse the repository at this point in the history
  • Loading branch information
Tibor Digana committed Sep 21, 2012
1 parent d48bf97 commit 41e5c7e
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions src/main/java/org/junit/experimental/ParallelComputer.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,22 @@ public static Computer methods() {
return new ParallelComputer(false, true);
}

private static <T> Runner parallelize(Runner runner) {
if (runner instanceof ParentRunner<?>) {
private static Runner parallelize(Runner runner) {
if (runner instanceof ParentRunner) {
((ParentRunner<?>) runner).setScheduler(new RunnerScheduler() {
private final List<Future<Object>> fResults= new ArrayList<Future<Object>>();
private final ExecutorService fService= Executors.newCachedThreadPool();

private final ExecutorService fService= Executors
.newCachedThreadPool();

public void schedule(final Runnable childStatement) {
fResults.add(fService.submit(new Callable<Object>() {
public Object call() throws Exception {
childStatement.run();
return null;
}
}));
public void schedule(Runnable childStatement) {
fService.submit(childStatement);
}

public void finished() {
for (Future<Object> each : fResults)
try {
each.get();
} catch (Exception e) {
e.printStackTrace();
}
try {
fService.shutdown();
fService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
} catch (InterruptedException e) {
e.printStackTrace(System.err);
}
}
});
}
Expand Down

0 comments on commit 41e5c7e

Please sign in to comment.