Advertisement
Guest User

Untitled

a guest
Aug 1st, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. long startTime = System.currentTimeMillis();
  2. List<Callable<String>> callables = new ArrayList<Callable<String>>();
  3. ExecutorService executor = Executors.newCachedThreadPool();
  4. callables.add(new Callable<String>() {
  5. public String call() {
  6. for (int i=0; i<100000; i++) {
  7. System.out.println("i "+i);
  8. }
  9. return "Task 1";
  10. }
  11. }
  12. );
  13.  
  14. callables.add(new Callable<String>() {
  15. public String call() {
  16. for (int j=0; j<100000; j++) {
  17. System.out.println("j "+j);
  18. }
  19. return "Task 2";
  20. }
  21. }
  22. );
  23.  
  24. callables.add(new Callable<String>() {
  25. public String call() {
  26. for (int k=0; k<100000; k++) {
  27. System.out.println("k "+k);
  28. }
  29. return "Task 3";
  30. }
  31. }
  32. );
  33. try {
  34. List<Future<String>> futureLst = executor.invokeAll(callables);
  35. for (Future future : futureLst) {
  36. try {
  37. System.out.println(future.get());
  38. } catch (ExecutionException e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. } catch (InterruptedException e) {
  43. e.printStackTrace();
  44. }
  45. executor.shutdown();
  46. System.out.println("Time Taken - "+ (System.currentTimeMillis() - startTime));
  47.  
  48. long startTime = System.currentTimeMillis();
  49. for (int i=0; i<100000; i++) {
  50. System.out.println("i "+i);
  51. }
  52. for (int j=0; j<100000; j++) {
  53. System.out.println("j "+j);
  54. }
  55. for (int k=0; k<100000; k++) {
  56. System.out.println("k "+k);
  57. }
  58. System.out.println("Time Taken - "+ (System.currentTimeMillis() - startTime));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement