2016-12-29 4 views
2

私は、URLから価格を取得し、それを整数に解析するために使用できる「パーサー」と呼ばれるクラスを持っています。URLをコンストラクタで同時に解析するには?

次に、これらの変数を使用してオブジェクトを作成する他のクラスがあります。問題は、連続して実行されているため、非常に遅いことです。

URLをパラレルに解析する方法を教えてください。

public class Parser { 
    public static int getPrice(String url) { 
     String price = ""; 
     try { 
      Document doc = Jsoup.connect(url).get(); 
      price = doc.select("h3").select("span").attr("title"); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return parseInt(price); 
    } 

    public static double parseDouble(String parseMe) { 
     NumberFormat ukFormat = NumberFormat.getNumberInstance(Locale.UK); 
     double parsed = 0; 
     try { 
      parsed = ukFormat.parse(parseMe).doubleValue(); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 
     return parsed; 
    } 
} 

//Here is an example of the class 
public class Example(){ 
    private int field1, field2; 

    public Example(String url1, String url2){ 
     field1=Parser.getPrice(url1); 
     field2=Parser.getPrice(url2); 
    } 
} 

答えて

1

、あなたはそうのように、ExecutorServiceを使用することができます。

public Example(String url1, String url2) { 
    // Create executorService. 
    ExecutorService executorService = Executors.newWorkStealingPool(); 

    // Submit both tasks to executorService. 
    Future<Integer> future1 = executorService.submit(new Callable<Integer>() { 
     @Override 
     public Integer call() throws Exception { 
      return Parser.getPrice(url1); 
     } 
    }); 
    Future<Integer> future2 = executorService.submit(new Callable<Integer>() { 
     @Override 
     public Integer call() throws Exception { 
      return Parser.getPrice(url2); 
     } 
    }); 

    // Shutdown executorService. (It will no longer accept tasks, but will complete the ones in progress.) 
    executorService.shutdown(); 

    // Handle results of the tasks. 
    try { 
     // Note: get() will block until the task is complete 
     field1 = future1.get(); 
     field2 = future2.get(); 
    } catch (InterruptedException e) { 
     // TODO Handle it 
    } catch (ExecutionException e) { 
     // TODO Handle it 
    } 
} 
0

は、私は、同じ機能に2つのURLを解析するためにそれらを持っていなければならなかった私にとっては、まったく同じケースがあったが、代わりに整数を返すのではなく、代わりに二つの整数の配列を返し、それがでしたより速く私はあなたのコードは次のようになりますような方法でCyclicBarrierで作業を示唆しているあなたのケースで :あなたが非同期的に実行するようgetPrice通話を希望した場合

final CyclicBarrier cb = new CyclicBarrier(2); // the parameter 2 is the number of threads that will invode the await method 

    long startTime = System.nanoTime();// this is just the start time to measure how many it took 
    Thread t1 = new Thread(){ 
     public void run(){ 
      try { 
       cb.await(); 
       int field1 = Parser.getPrice(url1); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } catch (BrokenBarrierException e) { 
       e.printStackTrace(); 
      } 

     }}; 

     Thread t2 = new Thread(){ 
      public void run(){ 
       try { 
        cb.await(); 
        int field2 = Parser.getPrice(url2); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } catch (BrokenBarrierException e) { 

        e.printStackTrace(); 
       } 

      }}; 

      t1.start(); 
      t2.start(); 
    long endTime = System.nanoTime();// end time of execution 
    long duration = (endTime - startTime); 
    System.out.println(duration); 
関連する問題