Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 0.54 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Implementing Threads via Futures
  2. ExecutorService pool = Executors.newFixedThreadPool(MAX_NSH_THREADS);
  3.     Set<Future<Void>> futureRequest = new HashSet<Future<Void>>();
  4.  
  5.     for (String host : SomeCollection)) {
  6.         Callable<Void> callable = new FileExtractor(j);
  7.         Future<Void> future = pool.submit(callable);
  8.         futureRequest.add(future);
  9.     }
  10.  
  11.     for (Future<Void> future : futureRequest) {
  12.         try {
  13.             future.get();
  14.         } catch (Exception e) {
  15.             logger.error(e);
  16.         }
  17.     }
  18.  
  19.     pool.shutdown();