Example usage for java.util.concurrent Callable Callable

List of usage examples for java.util.concurrent Callable Callable

Introduction

In this page you can find the example usage for java.util.concurrent Callable Callable.

Prototype

Callable

Source Link

Usage

From source file:com.mgmtp.perfload.core.client.util.concurrent.DelayingExecutorServiceTest.java

@Test
public void testWithDelay() throws InterruptedException, BrokenBarrierException {
    DelayingExecutorService execSrv = new DelayingExecutorService();

    final StopWatch sw = new StopWatch();

    final CyclicBarrier stopBarrier = new CyclicBarrier(11, new Runnable() {
        @Override/*from  ww w .j  a  va 2s.c om*/
        public void run() {
            sw.stop();
        }
    });

    sw.start();

    final long taskSleepMillis = 75L;
    long delayMultiplier = 50L;
    int loopMax = 10;

    for (int i = 0; i < loopMax; ++i) {
        Callable<Void> c = new Callable<Void>() {
            @Override
            public Void call() {
                try {
                    Thread.sleep(taskSleepMillis);
                    stopBarrier.await();
                } catch (Exception ex) {
                    throw new AssertionError(ex);
                }
                return null;
            }
        };

        long delay = delayMultiplier * i;

        ScheduledFuture<?> future = execSrv.schedule(c, delay, TimeUnit.MILLISECONDS);
        long actualDelay = future.getDelay(TimeUnit.MILLISECONDS);

        // compare with epsilon to make up for bad accuracy
        assertTrue(abs(delay - actualDelay) < EPSILON);
    }

    stopBarrier.await();

    long actualTime = sw.getTime();
    long expectedTime = delayMultiplier * (loopMax - 1) + taskSleepMillis;

    // compare with epsilon to make up for bad accuracy
    assertTrue(abs(actualTime - expectedTime) < EPSILON);
}

From source file:its.tools.SonarlintDaemon.java

public void waitReady() {
    await().atMost(10, TimeUnit.SECONDS).until(new Callable<Boolean>() {
        @Override/*from www  .j a  va  2  s . co m*/
        public Boolean call() throws Exception {
            return getOut().contains(READY_LOG);
        }
    });
}

From source file:com.tesora.dve.mysqlapi.repl.MyReplicationVisitorDispatch.java

public static void executeLDR(ServerDBConnection serverDBConnection,
        final ChannelHandlerContext channelHandlerContext, final byte[] query) throws SQLException {
    final MysqlLoadDataInfileRequestCollector resultConsumer = new MysqlLoadDataInfileRequestCollector(
            channelHandlerContext);/*w  w w  . ja  v  a2s .  c  om*/
    try {
        final NativeCharSet clientCharSet = MysqlNativeCharSet.UTF8;
        final SSConnection ssCon1 = serverDBConnection.getSSConn();
        Throwable t = ssCon1.executeInContext(new Callable<Throwable>() {
            public Throwable call() {
                try {
                    LoadDataRequestExecutor.execute(channelHandlerContext, ssCon1, resultConsumer,
                            clientCharSet.getJavaCharset(), query);
                } catch (Throwable e) {
                    return e;
                }
                return null;
            }
        });

        if (t != null && t.getCause() != null) {
            throw new PEException(t);
        }
        if (resultConsumer.getFileName() == null) {
            throw new SQLException(new PEException("Cannot handle load data statement: " + new String(query)));
        }
    } catch (Throwable t) {
        throw new SQLException(t);
    }
}

From source file:com.microsoft.azure.management.storage.UsageOperationsImpl.java

/**
* Gets the current usage count and the limit for the resources under the
* subscription.//from   w  ww . j a  v a 2s  .  c  o  m
*
* @return The List Usages operation response.
*/
@Override
public Future<UsageListResponse> listAsync() {
    return this.getClient().getExecutorService().submit(new Callable<UsageListResponse>() {
        @Override
        public UsageListResponse call() throws Exception {
            return list();
        }
    });
}

From source file:com.sishuok.chapter3.web.listener.ListenerController.java

@RequestMapping("/listener3")
@ResponseBody/*from  w  ww  . j  a va  2 s . co m*/
public WebAsyncTask<String> listener3() {
    long timeout = 10L * 1000; // 10
    final WebAsyncTask webAsyncTask = new WebAsyncTask(timeout, new Callable() {
        @Override
        public String call() throws Exception {
            Thread.sleep(2L * 1000);
            return "success";
        }
    });

    webAsyncTask.onTimeout(new Callable() {
        @Override
        public Object call() throws Exception {
            System.out.println("====");
            return "error";
        }
    });

    webAsyncTask.onCompletion(new Runnable() {
        @Override
        public void run() {
            System.out.println("===?");
        }
    });

    return webAsyncTask;
}

From source file:com.clxcommunications.xms.PagedFetcherTest.java

private static Callable<Page<Integer>> mockedFetchCallable(final List<List<Integer>> pages, final int pageNum) {
    int sizeAcc = 0;
    for (List<Integer> page : pages) {
        sizeAcc += page.size();//from  w w w  .j ava  2  s.c o m
    }
    final int totalSize = sizeAcc;

    return new Callable<Page<Integer>>() {

        @Override
        public Page<Integer> call() throws Exception {

            return new Page<Integer>() {

                @Override
                public int page() {
                    return pageNum;
                }

                @Override
                public int size() {
                    return pages.get(pageNum).size();
                }

                @Override
                public int totalSize() {
                    return totalSize;
                }

                @Override
                public List<Integer> content() {
                    return pages.get(pageNum);
                }

            };
        }

    };
}

From source file:com.microsoft.azure.subscriptions.SubscriptionOperationsImpl.java

/**
* Gets details about particular subscription.
*
* @param subscriptionId Required. Id of the subscription.
* @return Subscription detailed information.
*///from  w  w w .jav  a2 s  .c  o  m
@Override
public Future<GetSubscriptionResult> getAsync(final String subscriptionId) {
    return this.getClient().getExecutorService().submit(new Callable<GetSubscriptionResult>() {
        @Override
        public GetSubscriptionResult call() throws Exception {
            return get(subscriptionId);
        }
    });
}

From source file:org.apache.lucene.replicator.http.HttpReplicator.java

@Override
public InputStream obtainFile(String sessionID, String source, String fileName) throws IOException {
    String[] params = new String[] { ReplicationService.REPLICATE_SESSION_ID_PARAM, sessionID,
            ReplicationService.REPLICATE_SOURCE_PARAM, source, ReplicationService.REPLICATE_FILENAME_PARAM,
            fileName, };//from  w  ww. jav a 2s.  c o m
    final HttpResponse response = executeGET(ReplicationAction.OBTAIN.name(), params);
    return doAction(response, false, new Callable<InputStream>() {
        @Override
        public InputStream call() throws Exception {
            return responseInputStream(response, true);
        }
    });
}

From source file:com.microsoft.azure.management.compute.UsageOperationsImpl.java

/**
* Lists compute usages for a subscription.
*
* @param location Required. The location upon which resource usage is
* queried.//from  w ww . j  ava 2s  .c o  m
* @return The List Usages operation response.
*/
@Override
public Future<ListUsagesResponse> listAsync(final String location) {
    return this.getClient().getExecutorService().submit(new Callable<ListUsagesResponse>() {
        @Override
        public ListUsagesResponse call() throws Exception {
            return list(location);
        }
    });
}

From source file:de.unentscheidbar.validation.swing.trigger.DocumentChangeTriggerTest.java

@Override
protected void provokeTrigger(Iterable<JTextComponent> components) {

    for (final JTextComponent c : components) {
        runInEdt(new Callable<Void>() {

            @Override/*ww  w .j  a  v  a2  s. com*/
            public Void call() throws Exception {

                Document doc = c.getDocument();
                switch (rnd.nextInt(3)) {
                case 0:
                    doc.remove(doc.getStartPosition().getOffset(), doc.getLength() / 2);
                    break;
                case 1:
                    doc.insertString(rnd.nextInt(Math.max(1, doc.getStartPosition().getOffset())),
                            RandomStringUtils.randomAlphanumeric(1 + rnd.nextInt(10)), null);
                    break;
                case 2:
                    c.setText(UUID.randomUUID().toString());
                    break;
                default:
                    Assert.fail();
                }
                return null;
            }
        });

    }
    drainEventQueue();
}