Java 8 Future - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 Future class.

This code example shows how to use the following methods:cancel, get, isCancelled, isDone
        AsyncBean bean = lookup(AsyncBean.class);
        final CountDownLatch latch = new CountDownLatch(1);
        final CountDownLatch latch2 = new CountDownLatch(1);
        final Future<String> future = bean.asyncCancelMethod(latch, latch2);
        latch.await(WAIT_TIME_S, TimeUnit.SECONDS);
        Assert.assertFalse(future.isDone()); // we are in async method
        Assert.assertFalse(future.isCancelled());
        boolean wasCanceled = future.cancel(true); // we are running - task can't be canceled
        if (wasCanceled) {
            Assert.assertTrue("isDone() was expected to return true after a call to cancel() with mayBeInterrupting = true, returned true", future.isDone());
            Assert.assertTrue("isCancelled() was expected to return true after a call to cancel() returned true", future.isCancelled());
        } 
        latch2.countDown();
        String result = future.get();
        Assert.assertFalse(wasCanceled); // this should be false because task was not cancelled
        Assert.assertEquals("false;true", result); // the bean knows that it was cancelled
    } 
    
    @Test 
    @RunAsClient 
8
CodeRank
This code example shows how to use the following methods:cancel
    private double convertNsRate(double ratePerNs) {
        return ratePerNs * (double) rateUnit.toNanos(1);
    } 
 
    public void stop() { 
        future.cancel(false);
    } 
} 
8
CodeRank
This code example shows how to use the following methods:cancel, get, isCancelled, isDone
 
        replay(); 
 
        Future<String> future = executor.invoke(inv);
 
        assertFalse(future.cancel(true));
        assertFalse(future.isCancelled());
        assertTrue(future.isDone());
        assertSame(future.get(), value);
        assertSame(future.get(0, null), value);
 
        verify(); 
 
    } 
} 
 
 
7
CodeRank
This code example shows how to use the following methods:isDone
        return nodeFuture.isCancelled();
    } 
 
    @Override 
    public boolean isDone() { 
        return nodeFuture.isDone();
    } 
 
    @Override 
    public ServerDeploymentPlanResult get() throws InterruptedException, ExecutionException { 
        boolean cleanup = true;
        ModelNode node;
        try { 
            node = nodeFuture.get();
        } catch (InterruptedException ie) {
            cleanup = false;  // still may be in progress, so wait for finalize()
            throw ie;
        } finally { 
            if (cleanup) {
                plan.cleanup();
7
CodeRank
This code example shows how to use the following methods:cancel
            } 
        } 
 
        void safeCancel(Future<?> future) {
            if (future != null) {
                future.cancel(true);
            } 
        } 
    } 
} 
7
CodeRank
This code example shows how to use the following methods:cancel, get, isCancelled, isDone
    serverSocket.close();
    Future<Socket> connectorTask = executor.submit(connector);
 
    // this connection attempt will always timeout 
    try { 
      connectorTask.get(SHORT_DELAY, TimeUnit.MILLISECONDS);
      fail(); 
    } catch(TimeoutException e) {
    } 
    Exception lastException = exceptionHandler.awaitConnectionFailed(DELAY);
    assertTrue(lastException instanceof ConnectException);
    assertFalse(connectorTask.isDone());
    connectorTask.cancel(true);
 
    //thread.join(4 * DELAY); 
    assertTrue(connectorTask.isCancelled());
  } 
 
  @Test(timeout = 5000)
  public void testConnectEventually() throws Exception {
    serverSocket.close();
7
CodeRank
This code example shows how to use the following methods:get, isCancelled, isDone
 
	public void update() { 
		if (future == null)
			return; 
 
		if (!future.isDone())
			return; 
 
		try { 
 
			if (future.isCancelled()) {
				futureHandler.failed(null);
			} else { 
				futureHandler.done(future.get());
			} 
 
		} catch (InterruptedException e) {
			futureHandler.failed(e);
		} catch (ExecutionException e) {
			futureHandler.failed(e);
7
CodeRank
This code example shows how to use the following methods:cancel
 
    @Override 
    public void cancel(ImmutableSession session) {
        Future<?> future = this.expirationFutures.remove(session.getId());
        if (future != null) {
            future.cancel(false);
        } 
    } 
 
    @Override 
    public void schedule(ImmutableSession session) {
        long timeout = session.getMetaData().getMaxInactiveInterval(TimeUnit.MILLISECONDS);
        if (timeout > 0) {
            String id = session.getId();
            InfinispanWebLogger.ROOT_LOGGER.tracef("Session %s will expire in %d ms", id, timeout);
            this.expirationFutures.put(id, this.executor.schedule(new ExpirationTask(id), timeout, TimeUnit.MILLISECONDS));
        } 
    } 
 
    @Override 
7
CodeRank
This code example shows how to use the following methods:get
 
    @Test 
    public void testReturnAsyncInvocationReturningValue() throws Exception { 
        RemoteInterface remote = lookup(StatelessRemoteBean.class.getSimpleName(), RemoteInterface.class);
        Future<String> value = remote.hello();
        Assert.assertEquals("hello", value.get(5, TimeUnit.SECONDS));
    } 
} 
7
CodeRank
This code example shows how to use the following methods:isDone
 
	@Test
	public void scheduleAndCancelHeartbeat() throws Exception {
 
		ScheduledFuture<?> task = mock(ScheduledFuture.class);
		willReturn(task).given(this.taskScheduler).schedule(any(Runnable.class), any(Date.class));
 
		this.session.setActive(true);
		this.session.scheduleHeartbeat();
 
		verify(this.taskScheduler).schedule(any(Runnable.class), any(Date.class));
		verifyNoMoreInteractions(this.taskScheduler);
 
		given(task.isDone()).willReturn(false);
 
		this.session.cancelHeartbeat();
 
		verify(task).isDone();
		verify(task).cancel(false);
		verifyNoMoreInteractions(task);

8
CodeRank

Related Java 8 Future Questions & Answers:

See Code Examples for Java 8 Future Methods: