Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- long startTime = System.currentTimeMillis();
- List<Callable<String>> callables = new ArrayList<Callable<String>>();
- ExecutorService executor = Executors.newCachedThreadPool();
- callables.add(new Callable<String>() {
- public String call() {
- for (int i=0; i<100000; i++) {
- System.out.println("i "+i);
- }
- return "Task 1";
- }
- }
- );
- callables.add(new Callable<String>() {
- public String call() {
- for (int j=0; j<100000; j++) {
- System.out.println("j "+j);
- }
- return "Task 2";
- }
- }
- );
- callables.add(new Callable<String>() {
- public String call() {
- for (int k=0; k<100000; k++) {
- System.out.println("k "+k);
- }
- return "Task 3";
- }
- }
- );
- try {
- List<Future<String>> futureLst = executor.invokeAll(callables);
- for (Future future : futureLst) {
- try {
- System.out.println(future.get());
- } catch (ExecutionException e) {
- e.printStackTrace();
- }
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- executor.shutdown();
- System.out.println("Time Taken - "+ (System.currentTimeMillis() - startTime));
- long startTime = System.currentTimeMillis();
- for (int i=0; i<100000; i++) {
- System.out.println("i "+i);
- }
- for (int j=0; j<100000; j++) {
- System.out.println("j "+j);
- }
- for (int k=0; k<100000; k++) {
- System.out.println("k "+k);
- }
- System.out.println("Time Taken - "+ (System.currentTimeMillis() - startTime));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement