Skip to content

Instantly share code, notes, and snippets.

@AndrienkoAleksandr
Last active August 29, 2015 14:05
Show Gist options
  • Save AndrienkoAleksandr/747b5de2494f78e9be35 to your computer and use it in GitHub Desktop.
Save AndrienkoAleksandr/747b5de2494f78e9be35 to your computer and use it in GitHub Desktop.
Callable without ExecutorService2
public class CallableWithoutExecutorService2 {
public static class WordLengthCallable implements Callable {
public static int count = 0;
private final int numberOfThread = count++;
public Integer call() throws InterruptedException {
int sum = 0;
for (int i = 0; i < 100000; i++) {
sum += i;
}
System.out.println(numberOfThread);
return numberOfThread;
}
}
public static void main(String[] args) throws InterruptedException {
List<FutureTask<Integer>> list = new ArrayList<FutureTask<Integer>>(
Arrays.asList(new FutureTask<Integer>(new WordLengthCallable()),
new FutureTask<Integer>(new WordLengthCallable()),
new FutureTask<Integer>(new WordLengthCallable()),
new FutureTask<Integer>(new WordLengthCallable()))
);
for (FutureTask<Integer> elem: list) {
new Thread(elem).start();
}
int sum = 0;
for (FutureTask<Integer> elem: list) {
try {
sum += elem.get().intValue();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
System.out.println("Result = " + sum);
System.exit(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment