Пример #1
0
  /**
   * We will test this by setting up two threads, asserting on the hash values the instances
   * generate for each thread
   *
   * @throws InterruptedException
   * @throws ExecutionException
   */
  @Test
  public void test_001_concurrent_match() throws InterruptedException, ExecutionException {

    // Setup callable method that reports the hashcode for the thread
    int threadCount = 2;
    Callable<Integer> task =
        new Callable<Integer>() {
          @Override
          public Integer call() {
            return Match.getInstance().hashCode();
          }
        };

    // Create n tasks to execute
    List<Callable<Integer>> tasks = Collections.nCopies(threadCount, task);

    // Execute the task for both tasks
    ExecutorService es = Executors.newFixedThreadPool(threadCount);
    List<Future<Integer>> futures = es.invokeAll(tasks);
    int hash1 = futures.get(0).get();
    int hash2 = futures.get(1).get();

    // The two hashcodes must NOT be equal
    Assert.assertThat(hash1, not(equalTo(hash2)));
  }
Пример #2
0
 @Test
 public void testInvokeAllTimeoutCancelled() throws Exception {
   ExecutorService executor = createSingleNodeExecutorService("testInvokeAll");
   assertFalse(executor.isShutdown());
   // Only one task
   ArrayList<Callable<Boolean>> tasks = new ArrayList<Callable<Boolean>>();
   tasks.add(new CancellationAwareTask(0));
   List<Future<Boolean>> futures = executor.invokeAll(tasks, 5, TimeUnit.SECONDS);
   assertEquals(futures.size(), 1);
   assertEquals(futures.get(0).get(), Boolean.TRUE);
   // More tasks
   tasks.clear();
   for (int i = 0; i < COUNT; i++) {
     tasks.add(new CancellationAwareTask(i < 2 ? 0 : 20000));
   }
   futures = executor.invokeAll(tasks, 5, TimeUnit.SECONDS);
   assertEquals(futures.size(), COUNT);
   for (int i = 0; i < COUNT; i++) {
     if (i < 2) {
       assertEquals(futures.get(i).get(), Boolean.TRUE);
     } else {
       boolean excepted = false;
       try {
         futures.get(i).get();
       } catch (CancellationException e) {
         excepted = true;
       }
       assertTrue(excepted);
     }
   }
 }
Пример #3
0
  public static void main(String[] args) throws InterruptedException, ExecutionException {

    List<Future<String>> futures = new ArrayList<>();
    ExecutorService executor = Executors.newFixedThreadPool(5);
    for (int i = 1000; i <= 1010; i++) {
      futures.add(executor.submit(getTask(i)));
    }

    futures.add(executor.submit(getTask(10000000)));

    for (Future<String> future : futures) {
      System.out.println(future.get());
    }

    executor.shutdown();
    executor.awaitTermination(10, TimeUnit.SECONDS);

    /* output
    500500
    501501
    502503
    503506
    504510
    505515
    506521
    507528
    508536
    509545
    510555
    50000005000000
    */
  }
Пример #4
0
 @AfterClass
 public static void tearDown() throws InterruptedException, ExecutionException, IOException {
   DirDataImpl dirData = new DirDataImpl();
   DirDataImpl.setNumber(3334);
   List<String> paths = dirData.dividePath(rootPath, 30, THREADS);
   List<Future<String>> list = new ArrayList<>();
   try {
     for (String p : paths) {
       Future<String> submit =
           executor.submit(
               () -> {
                 dirData.delDir(p, 6);
                 return "It done!";
               });
       list.add(submit);
     }
     for (Future<String> future : list) {
       future.get();
     }
   } finally {
     executor.shutdown();
     while (!executor.awaitTermination(10, TimeUnit.SECONDS)) {
       LOG.info("Awaiting completion of threads.");
     }
   }
   FileUtils.deleteDirectory(new File(rootPath));
 }
  @Override
  public Boolean call() throws Exception {

    long timeoutMillis = 5000;

    try {
      getServersFile();
      getZkRunning();

      while (true) {
        while (!restartQueue.isEmpty()) {
          LOG.debug("Restart queue size [" + restartQueue.size() + "]");
          RestartHandler handler = restartQueue.poll();
          Future<ScriptContext> runner = pool.submit(handler);
          ScriptContext scriptContext = runner.get(); // blocking call
          if (scriptContext.getExitCode() != 0) restartQueue.add(handler);
        }

        try {
          Thread.sleep(timeoutMillis);
        } catch (InterruptedException e) {
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
      LOG.error(e);
      pool.shutdown();
      throw e;
    }
  }
  public static void main(String[] args) {
    ExecutorService pool = Executors.newFixedThreadPool(2);

    final Vector[] vecs = {
      new Vector(), new Vector(),
    };
    vecs[0].add(vecs[1]);
    vecs[1].add(vecs[0]);

    for (int i = 0; i < 2; i++) {
      final int threadNumber = i;
      pool.submit(
          new Callable() {
            public Object call() throws Exception {
              for (int i = 0; i < 1000 * 1000; i++) {
                ObjectOutputStream out = new ObjectOutputStream(new NullOutputStream());
                out.writeObject(vecs[threadNumber]);
                out.close();
              }
              System.out.println("done");
              return null;
            }
          });
    }
  }
Пример #7
0
  public static void main(String[] args) throws Exception {
    ExecutorService exec = Executors.newCachedThreadPool();
    for (int i = 0; i < 5; i++) exec.execute(new Task());
    exec.execute(new Task2());
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(
        new TimerTask() {
          boolean prod = true;

          public void run() {
            if (prod) {
              System.out.print("\nnotify() ");
              Task.blocker.prod();
              prod = false;
            } else {
              System.out.print("\nnotifyAll() ");
              Task.blocker.prodAll();
              prod = true;
            }
          }
        },
        400,
        400); // Run every .4 second
    TimeUnit.SECONDS.sleep(5); // Run for a while...
    timer.cancel();
    System.out.println("\nTimer canceled");
    TimeUnit.MILLISECONDS.sleep(500);
    System.out.print("Task2.blocker.prodAll() ");
    Task2.blocker.prodAll();
    TimeUnit.MILLISECONDS.sleep(500);
    System.out.println("\nShutting down");
    exec.shutdownNow(); // Interrupt all tasks
  }
Пример #8
0
  public static <T> List<T> executeAndGetResult(
      Collection<? extends Callable<T>> tasks, long waitingTimeInMillis) {
    ExecutorService executor = Executors.newFixedThreadPool(tasks.size());
    List<T> finalResults = new ArrayList<T>(tasks.size());
    List<Future<T>> temporaryResults = new ArrayList<Future<T>>(tasks.size());

    try {
      for (Callable<T> task : tasks) {
        Future<T> future = executor.submit(task);
        temporaryResults.add(future);
      }
      executor.awaitTermination(waitingTimeInMillis, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
      throw new RuntimeException(e);
    } finally {
      executor.shutdown();
    }

    finalResults = new ArrayList<T>(temporaryResults.size());

    for (Future<T> result : temporaryResults) {
      try {
        finalResults.add(result.get());
      } catch (InterruptedException e) {
        throw new RuntimeException(e);
      } catch (ExecutionException e) {
        throw new RuntimeException(e);
      }
    }

    return finalResults;
  }
  /* Regression test for DB-3614 */
  @Test
  @Category(SlowTest.class)
  @Ignore("-sf- takes way too long to fail and interferes rest of build")
  public void testTenTableJoinExplainDuration() throws Exception {
    int size = 10;
    List<String> tables = new ArrayList<String>(size);
    List<String> joins = new ArrayList<String>(size - 1);
    for (int i = 0; i < size; i++) {
      methodWatcher.executeUpdate(format("create table tentab%s (c1 int primary key)", i));
      methodWatcher.executeUpdate(format("insert into tentab%s values (1)", i));
      tables.add(format("tentab%s", i));
      if (i > 0) {
        joins.add(format("tentab%s.c1 = tentab%s.c1", i, i - 1));
      }
    }
    System.out.println("Tables created");
    final String fromClause = Joiner.on(", ").join(tables);
    final String joinCriteria = Joiner.on(" AND ").join(joins);

    ExecutorService es =
        Executors.newSingleThreadExecutor(
            new ThreadFactory() {
              @Override
              public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setDaemon(true);
                return t;
              }
            });
    try {
      final CyclicBarrier startLatch = new CyclicBarrier(2);
      final CountDownLatch finishLatch = new CountDownLatch(1);
      Future<Void> f =
          es.submit(
              new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                  String query =
                      format("EXPLAIN SELECT * FROM %s WHERE %s ", fromClause, joinCriteria);
                  startLatch.await();
                  try {
                    ResultSet rs = methodWatcher.executeQuery(query);
                    // Loose check that explain statement took a few seconds or less,
                    // because we're just making sure the short circuit logic in
                    // OptimizerImpl.checkTimeout() blocks this from taking several minutes.
                    Assert.assertTrue("Explain did not return result!", rs.next());
                  } finally {
                    finishLatch.countDown();
                  }
                  return null;
                }
              });
      System.out.println("Starting wait");
      startLatch.await();
      f.get(1, TimeUnit.SECONDS);
      System.out.println("Finished waiting");
    } finally {
      System.out.println("shutting down");
    }
  }
  private int[] computeClusterNumberRange(int min, int max, int hop, Optimizer optimizer)
      throws ExecutionException, InterruptedException {
    ExecutorService executor;
    if (parallelWorkers > 1) {
      executor = Executors.newFixedThreadPool(2);
    } else {
      executor = Executors.newFixedThreadPool(1);
    }
    ConcurrentMap<Integer, Double> sharedScores = new ConcurrentHashMap<>();
    SearchSpaceBoundaryFinder_old upperBoundFinder =
        new SearchSpaceUpperBoundaryFinder_old(min, max, hop, optimizer, sharedScores);
    Future<Integer> futureUpperBound = executor.submit(upperBoundFinder);
    SearchSpaceBoundaryFinder_old lowerBoundFinder =
        new SearchSpaceLowerBoundaryFinder_old(min, max, hop, optimizer, sharedScores);
    Future<Integer> futureLowerBound = executor.submit(lowerBoundFinder);
    executor.shutdown();
    int[] r = new int[2];
    Integer realMin = futureLowerBound.get();
    Integer realMax = futureUpperBound.get();
    r[0] = realMin == null ? -1 : realMin;
    r[1] = realMax == null ? -1 : realMax;

    scores.putAll(lowerBoundFinder.getSplitsAndScores());
    // scores.putAll(upperBoundFinder.getSplitsAndScores());
    return r;
  }
Пример #11
0
  public static void main(String[] args) throws Exception {
    parseArguments(args);

    List<Iterator<String>> segments = Splitter.split(inputFilePath, numberOfThreads);

    List<Future<Analytic>> futures = new ArrayList<Future<Analytic>>(segments.size());
    ExecutorService threadPool = Executors.newFixedThreadPool(segments.size());
    for (final Iterator<String> segment : segments) {
      Future<Analytic> future =
          threadPool.submit(
              new Callable<Analytic>() {
                @Override
                public Analytic call() throws Exception {
                  return new Analyzer(segment).parse();
                }
              });
      futures.add(future);
    }

    try {
      ArrayList<Analytic> analytics = new ArrayList<Analytic>(futures.size());
      for (Future<Analytic> future : futures) {
        analytics.add(future.get());
      }
      Analytic result = Synthesizer.synthesize(analytics);
      writeResult(result);
    } finally {
      threadPool.shutdown();
    }
  }
Пример #12
0
 /** Execute a task that is executing something else inside. Nested Execution. */
 @Test(timeout = 10000)
 public void testNestedExecution() throws Exception {
   Callable<String> task = new NestedExecutorTask();
   ExecutorService executor = createSingleNodeExecutorService("testNestedExecution");
   Future future = executor.submit(task);
   future.get();
 }
Пример #13
0
  /**
   * Test concurrent reader and writer (GH-458).
   *
   * <p><b>Test case:</b>
   *
   * <ol>
   *   <li/>start a long running reader;
   *   <li/>try to start a writer: it should time out;
   *   <li/>stop the reader;
   *   <li/>start the writer again: it should succeed.
   * </ol>
   *
   * @throws Exception error during request execution
   */
  @Test
  @Ignore("There is no way to stop a query on the server!")
  public void testReaderWriter() throws Exception {
    final String readerQuery = "?query=(1%20to%20100000000000000)%5b.=1%5d";
    final String writerQuery = "/test.xml";
    final byte[] content = Token.token("<a/>");

    final Get readerAction = new Get(readerQuery);
    final Put writerAction = new Put(writerQuery, content);

    final ExecutorService exec = Executors.newFixedThreadPool(2);

    // start reader
    exec.submit(readerAction);
    Performance.sleep(TIMEOUT); // delay in order to be sure that the reader has started
    // start writer
    Future<HTTPResponse> writer = exec.submit(writerAction);

    try {
      final HTTPResponse result = writer.get(TIMEOUT, TimeUnit.MILLISECONDS);

      if (result.status.isSuccess()) fail("Database modified while a reader is running");
      throw new Exception(result.toString());
    } catch (final TimeoutException e) {
      // writer is blocked by the reader: stop it
      writerAction.stop = true;
    }

    // stop reader
    readerAction.stop = true;

    // start the writer again
    writer = exec.submit(writerAction);
    assertEquals(HTTPCode.CREATED, writer.get().status);
  }
Пример #14
0
 /** Run a basic task */
 @Test
 public void testBasicTask() throws Exception {
   Callable<String> task = new BasicTestTask();
   ExecutorService executor = createSingleNodeExecutorService("testBasicTask");
   Future future = executor.submit(task);
   assertEquals(future.get(), BasicTestTask.RESULT);
 }
  @Override
  public void executeTest() throws Exception {

    ODatabaseDocumentTx database =
        poolFactory.get(getDatabaseURL(serverInstance.get(0)), "admin", "admin").acquire();
    System.out.println("Creating Writers and Readers threads...");

    final ExecutorService writerExecutors = Executors.newCachedThreadPool();

    runningWriters = new CountDownLatch(serverInstance.size() * writerCount);

    int serverId = 0;
    int threadId = 0;
    List<Callable<Void>> writerWorkers = new ArrayList<Callable<Void>>();
    for (ServerRun server : serverInstance) {
      for (int j = 0; j < writerCount; j++) {
        Callable writer = createWriter(serverId, threadId++, getDatabaseURL(server));
        writerWorkers.add(writer);
      }
      serverId++;
    }
    List<Future<Void>> futures = writerExecutors.invokeAll(writerWorkers);

    System.out.println("Threads started, waiting for the end");

    for (Future<Void> future : futures) {
      future.get();
    }

    writerExecutors.shutdown();
    Assert.assertTrue(writerExecutors.awaitTermination(1, TimeUnit.MINUTES));

    System.out.println("All writer threads have finished, shutting down readers");
  }
Пример #16
0
  // Concurrent insertion & then iterator test.
  public static void testNonBlockingIdentityHashMapIterator() throws InterruptedException {
    final int ITEM_COUNT1 = 1000;
    final int THREAD_COUNT = 5;
    final int PER_CNT = ITEM_COUNT1 / THREAD_COUNT;
    final int ITEM_COUNT = PER_CNT * THREAD_COUNT; // fix roundoff for odd thread counts

    NonBlockingIdentityHashMap<Long, TestKey> nbhml =
        new NonBlockingIdentityHashMap<Long, TestKey>();
    // use a barrier to open the gate for all threads at once to avoid rolling
    // start and no actual concurrency
    final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT);
    final ExecutorService ex = Executors.newFixedThreadPool(THREAD_COUNT);
    final CompletionService<Object> co = new ExecutorCompletionService<Object>(ex);
    for (int i = 0; i < THREAD_COUNT; i++) {
      co.submit(new NBHMLFeeder(nbhml, PER_CNT, barrier, i * PER_CNT));
    }
    for (int retCount = 0; retCount < THREAD_COUNT; retCount++) {
      co.take();
    }
    ex.shutdown();

    assertEquals("values().size()", ITEM_COUNT, nbhml.values().size());
    assertEquals("entrySet().size()", ITEM_COUNT, nbhml.entrySet().size());
    int itemCount = 0;
    for (TestKey K : nbhml.values()) itemCount++;
    assertEquals("values().iterator() count", ITEM_COUNT, itemCount);
  }
Пример #17
0
    public NonBlockingIdentityHashMap<Long, TestKey> getMapMultithreaded()
        throws InterruptedException, ExecutionException {
      final int threadCount = _items.keySet().size();
      final NonBlockingIdentityHashMap<Long, TestKey> map =
          new NonBlockingIdentityHashMap<Long, TestKey>();

      // use a barrier to open the gate for all threads at once to avoid rolling start and no actual
      // concurrency
      final CyclicBarrier barrier = new CyclicBarrier(threadCount);
      final ExecutorService ex = Executors.newFixedThreadPool(threadCount);
      final CompletionService<Integer> co = new ExecutorCompletionService<Integer>(ex);
      for (Integer type : _items.keySet()) {
        // A linked-list of things to insert
        List<TestKey> items = _items.get(type);
        TestKeyFeederThread feeder = new TestKeyFeederThread(type, items, map, barrier);
        co.submit(feeder);
      }

      // wait for all threads to return
      int itemCount = 0;
      for (int retCount = 0; retCount < threadCount; retCount++) {
        final Future<Integer> result = co.take();
        itemCount += result.get();
      }
      ex.shutdown();
      return map;
    }
Пример #18
0
  public static void shutdown() {
    logger_.info("Shutting down ...");
    synchronized (MessagingService.class) {
      /* Stop listening on any socket */
      for (SelectionKey skey : listenSockets_.values()) {
        SelectorManager.getSelectorManager().cancel(skey);
      }
      listenSockets_.clear();

      /* Shutdown the threads in the EventQueue's */
      messageDeserializationExecutor_.shutdownNow();
      messageSerializerExecutor_.shutdownNow();
      messageDeserializerExecutor_.shutdownNow();
      streamExecutor_.shutdownNow();

      /* shut down the cachetables */
      taskCompletionMap_.shutdown();
      callbackMap_.shutdown();

      /* Interrupt the selector manager thread */
      SelectorManager.getSelectorManager().interrupt();

      poolTable_.clear();
      verbHandlers_.clear();
      bShutdown_ = true;
    }
    logger_.debug("Shutdown invocation complete.");
  }
Пример #19
0
  public static void main(String[] args) {
    ExecutorService executor = Executors.newCachedThreadPool();
    CompletionService<String> service = new ExecutorCompletionService<String>(executor);

    ReportRequest faceRequest = new ReportRequest("Face", service);
    ReportRequest onlineRequest = new ReportRequest("Online", service);
    Thread faceThread = new Thread(faceRequest);
    Thread onlineThread = new Thread(onlineRequest);

    ReportProcessor processor = new ReportProcessor(service);
    Thread processThread = new Thread(processor);

    System.out.println("Main: Starting the Threads");
    faceThread.start();
    onlineThread.start();
    processThread.start();

    try {
      System.out.println("Main: Waiting for the report generators.");
      faceThread.join();
      onlineThread.join();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    System.out.println("Main: Shutting down the executor.");
    executor.shutdown();
    try {
      executor.awaitTermination(1, TimeUnit.DAYS);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    processor.setEnd(true);
    System.out.println("Main: Ends");
  }
 public void start() throws InterruptedException {
   barrier = new CyclicBarrier(numberOfCars, this);
   for (int i = 0; i < numberOfCars; i++) {
     pool.execute(new Car("Car" + i, 100 + random.nextInt(100), this));
   }
   pool.awaitTermination(300, TimeUnit.MILLISECONDS);
 }
Пример #21
0
  public static void main(String[] args) {
    Callable<Integer> task =
        () -> {
          try {
            TimeUnit.SECONDS.sleep(1);
            return 123;
          } catch (InterruptedException e) {
            throw new IllegalStateException("task interrupted", e);
          }
        };

    ExecutorService executor = Executors.newFixedThreadPool(1);
    Future<Integer> future = executor.submit(task);
    System.out.println("future done? " + future.isDone());
    Integer result = -1;
    try {
      result = future.get();
    } catch (Exception e) {
      e.printStackTrace();
    }
    System.out.println("future done? " + future.isDone());
    System.out.println("result: " + result);

    stop(executor);
  }
 public static void main(String[] args) throws Exception {
   ExecutorService exec = Executors.newCachedThreadPool();
   for (int i = 0; i < N_ELEMENTS; i++)
     for (int j = 0; j < N_GENES; j++) GRID[i][j] = new AtomicInteger(rand.nextInt(1000));
   for (int i = 0; i < N_EVOLVERS; i++) exec.execute(new Evolver());
   TimeUnit.SECONDS.sleep(5);
   exec.shutdownNow();
 }
 public static void main(String[] args) throws Exception {
   Car car = new Car();
   ExecutorService exec = Executors.newCachedThreadPool();
   exec.execute(new WaxOff(car));
   exec.execute(new WaxOn(car));
   TimeUnit.SECONDS.sleep(5); // Run for a while...
   exec.shutdownNow(); // Interrupt all tasks
 }
Пример #24
0
 public static void test(IntGenerator go, int count) {
   System.out.println("Press Control-C to exit");
   ExecutorService exec = Executors.newCachedThreadPool();
   for (int i = 0; i < count; i++) {
     exec.execute(new EventChecker(go, i));
   }
   exec.shutdown();
 }
Пример #25
0
 /** Demonstrating the failing of the pre/post conditions of Even. */
 public static void main(String[] args) {
   ExecutorService e = Executors.newFixedThreadPool(10);
   Even ev = new Even();
   for (int i = 0; i < 10; i++) {
     e.execute(new EvenTask(ev));
   }
   e.shutdown();
 }
Пример #26
0
 public MessageSender(UniqueValue unique, int port) throws IOException {
   this.unique = unique;
   executor.submit(tcpListener = new TcpListener(port, this::acceptMessage));
   executor.submit(udpListener = new UdpListener(port, this::acceptMessage));
   executor.submit(tcpDispatcher);
   executor.submit(udpDispatcher);
   executor.submit(new IncomeMessagesProcessor());
 }
Пример #27
0
 public <T> Collection<T> getParallelResults(List<Node<T>> nodes) throws InterruptedException {
   ExecutorService exec = Executors.newCachedThreadPool();
   Queue<T> resultQueue = new ConcurrentLinkedQueue<T>();
   parallelRecursive(exec, nodes, resultQueue);
   exec.shutdown();
   exec.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
   return resultQueue;
 }
 public static void main(String[] args) {
   try {
     ExecutorService exec = Executors.newCachedThreadPool();
     exec.execute(new ExceptionThread());
   } catch (RuntimeException ue) {
     // This statement will NOT execute!
     System.out.println("Exception has been handled!");
   }
 }
Пример #29
0
  @Override
  public BxDocument segmentDocument(BxDocument document) throws AnalysisException {
    Map<BxPage, List<Component>> componentMap = new HashMap<BxPage, List<Component>>();

    ExecutorService exec = Executors.newFixedThreadPool(PdfNLMContentExtractor.THREADS_NUMBER);
    ArrayList<Callable<NumBxPage>> tasks = new ArrayList<Callable<NumBxPage>>();
    for (BxPage page : document.getPages()) {
      tasks.add(new ComponentCounter(page));
    }

    List<Future<NumBxPage>> results;
    try {
      results = exec.invokeAll(tasks);
      exec.shutdown();

      for (Future<NumBxPage> result : results) {
        NumBxPage p = result.get();
        componentMap.put(p.page, p.components);
      }
    } catch (ExecutionException ex) {
      throw new AnalysisException("Cannot segment pages!", ex);
    } catch (InterruptedException ex) {
      throw new AnalysisException("Cannot segment pages!", ex);
    }

    this.computeDocumentOrientation(componentMap);

    BxDocument output = new BxDocument();
    BxPage[] pages = new BxPage[document.getPages().size()];

    exec = Executors.newFixedThreadPool(PdfNLMContentExtractor.THREADS_NUMBER);
    tasks = new ArrayList<Callable<NumBxPage>>();
    int i = 0;
    for (BxPage page : document.getPages()) {
      tasks.add(new SingleSegmenter(page, i++));
    }

    try {
      results = exec.invokeAll(tasks);
      exec.shutdown();

      for (Future<NumBxPage> result : results) {
        NumBxPage p = result.get();
        pages[p.index] = p.page;
      }
      for (BxPage p : pages) {
        if (p.getBounds() != null) {
          output.addPage(p);
        }
      }
      return output;
    } catch (ExecutionException ex) {
      throw new AnalysisException("Cannot segment pages!", ex);
    } catch (InterruptedException ex) {
      throw new AnalysisException("Cannot segment pages!", ex);
    }
  }
Пример #30
0
  @Test
  public void testPostregisteredExecutionCallbackCompletableFuture() throws Exception {
    HazelcastInstanceProxy proxy = (HazelcastInstanceProxy) createHazelcastInstance();
    Field originalField = HazelcastInstanceProxy.class.getDeclaredField("original");
    originalField.setAccessible(true);
    HazelcastInstanceImpl hz = (HazelcastInstanceImpl) originalField.get(proxy);
    NodeEngine nodeEngine = hz.node.nodeEngine;
    ExecutionService es = nodeEngine.getExecutionService();

    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final ExecutorService executorService = Executors.newSingleThreadExecutor();
    try {
      Future future =
          executorService.submit(
              new Callable<String>() {
                @Override
                public String call() {
                  try {
                    return "success";
                  } finally {
                    latch1.countDown();
                  }
                }
              });

      final ICompletableFuture completableFuture = es.asCompletableFuture(future);
      latch1.await(30, TimeUnit.SECONDS);

      final AtomicReference reference = new AtomicReference();
      completableFuture.andThen(
          new ExecutionCallback() {
            @Override
            public void onResponse(Object response) {
              reference.set(response);
              latch2.countDown();
            }

            @Override
            public void onFailure(Throwable t) {
              reference.set(t);
              latch2.countDown();
            }
          });

      latch2.await(30, TimeUnit.SECONDS);
      if (reference.get() instanceof Throwable) {
        ((Throwable) reference.get()).printStackTrace();
      }

      assertEquals("success", reference.get());

    } finally {
      executorService.shutdown();
    }
  }