Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.infobip.jsouptest;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.List;
- import java.util.concurrent.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- /**
- * Hello world!
- *
- */
- public class App
- {
- public static Document connect(String url) {
- Document doc = null;
- try {
- doc = Jsoup.connect(url).timeout(0).get();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- return doc;
- }
- public static void main( String[] args ) throws IOException
- {
- //
- //
- // ExecutorService executorService = Executors.newFixedThreadPool(5);
- // List<Future<Void>> handles = new ArrayList<Future<Void>>();
- // Future<Void> handle;
- // for (int i=0;i < 12; i++) {
- // handle = executorService.submit(new Callable<Void>() {
- //
- // public Void call() throws Exception {
- // System.out.println(App.connect("http://www.google.hr").title());
- // return null;
- // }
- // });
- // handles.add(handle);
- // }
- //
- // for (Future<Void> h : handles) {
- // try {
- // h.get();
- // }
- // catch (Exception ex) {
- // ex.printStackTrace();
- // }
- // }
- //
- // executorService.shutdownNow();
- Collection<Match> matches = new ArrayList<Match>();
- Collection<Future<Match>> results = new ArrayList<Future<Match>>();
- String[] elements = new String[12];
- Arrays.fill(elements, "http://www.google.hr");
- for (String element : elements) {
- MatchWorker matchWorker = new MatchWorker(element);
- FutureTask<Match> task = new FutureTask<Match>(matchWorker);
- results.add(task);
- Thread matchThread = new Thread(task);
- matchThread.start();
- }
- for(Future<Match> match : results) {
- try {
- matches.add(match.get());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- for (Match m : matches) {
- System.out.println(m);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement