Advertisement
Guest User

Main

a guest
Oct 6th, 2012
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. package com.infobip.jsouptest;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.Collection;
  7. import java.util.List;
  8. import java.util.concurrent.*;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11. import org.jsoup.Jsoup;
  12. import org.jsoup.nodes.Document;
  13.  
  14. /**
  15. * Hello world!
  16. *
  17. */
  18. public class App
  19. {
  20.  
  21. public static Document connect(String url) {
  22. Document doc = null;
  23. try {
  24. doc = Jsoup.connect(url).timeout(0).get();
  25. } catch (IOException ex) {
  26. ex.printStackTrace();
  27. }
  28. return doc;
  29. }
  30.  
  31.  
  32. public static void main( String[] args ) throws IOException
  33. {
  34.  
  35. //
  36. //
  37. // ExecutorService executorService = Executors.newFixedThreadPool(5);
  38. // List<Future<Void>> handles = new ArrayList<Future<Void>>();
  39. // Future<Void> handle;
  40. // for (int i=0;i < 12; i++) {
  41. // handle = executorService.submit(new Callable<Void>() {
  42. //
  43. // public Void call() throws Exception {
  44. // System.out.println(App.connect("http://www.google.hr").title());
  45. // return null;
  46. // }
  47. // });
  48. // handles.add(handle);
  49. // }
  50. //
  51. // for (Future<Void> h : handles) {
  52. // try {
  53. // h.get();
  54. // }
  55. // catch (Exception ex) {
  56. // ex.printStackTrace();
  57. // }
  58. // }
  59. //
  60. // executorService.shutdownNow();
  61.  
  62. Collection<Match> matches = new ArrayList<Match>();
  63. Collection<Future<Match>> results = new ArrayList<Future<Match>>();
  64.  
  65. String[] elements = new String[12];
  66. Arrays.fill(elements, "http://www.google.hr");
  67.  
  68. for (String element : elements) {
  69. MatchWorker matchWorker = new MatchWorker(element);
  70. FutureTask<Match> task = new FutureTask<Match>(matchWorker);
  71. results.add(task);
  72.  
  73. Thread matchThread = new Thread(task);
  74. matchThread.start();
  75. }
  76. for(Future<Match> match : results) {
  77. try {
  78. matches.add(match.get());
  79. } catch (Exception e) {
  80. e.printStackTrace();
  81. }
  82. }
  83. for (Match m : matches) {
  84. System.out.println(m);
  85. }
  86.  
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement