Example usage for java.util.concurrent ExecutorService submit

List of usage examples for java.util.concurrent ExecutorService submit

Introduction

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

Prototype

Future<?> submit(Runnable task);

Source Link

Document

Submits a Runnable task for execution and returns a Future representing that task.

Usage

From source file:com.blacklocus.qs.worker.util.log.SamplingQSLogServiceTest.java

@Test
public void testSampledLifeCycle() throws InterruptedException {

    // With these parameters, by far most logger interactions should be filtered out, very few sampled in.
    final int numThreads = 64, iterations = 200, processingJitterMaxMs = 16, noSoonerThanMs = 100;

    final Set<String> sampledTaskIds = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());

    final QSLogService logService = Mockito.mock(QSLogService.class);
    // track which logging interactions were allowed through (sampled in)
    Mockito.doAnswer(new Answer() {
        @Override//from  ww  w .j  a v  a  2  s  .  c o  m
        public Object answer(InvocationOnMock invocation) throws Throwable {
            sampledTaskIds.add(((QSTaskModel) invocation.getArguments()[0]).taskId);
            return null;
        }
    }).when(logService).startedTask(Matchers.any(QSTaskModel.class));
    Mockito.doAnswer(new Answer() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            sampledTaskIds.add(((QSLogModel) invocation.getArguments()[0]).taskId);
            return null; //TODO jason
        }
    }).when(logService).log(Matchers.any(QSLogModel.class));
    Mockito.doAnswer(new Answer() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            sampledTaskIds.add(((QSTaskModel) invocation.getArguments()[0]).taskId);
            return null; //TODO jason
        }
    }).when(logService).completedTask(Matchers.any(QSTaskModel.class));

    Predicate<QSTaskModel> taskPredicate = SamplingPredicates.noSoonerThan(noSoonerThanMs,
            TimeUnit.MILLISECONDS);
    final QSLogService sampledLogService = new SamplingQSLogService(logService, taskPredicate);

    long startNs = System.nanoTime();
    ExecutorService threads = Executors.newFixedThreadPool(numThreads);
    for (int i = 0; i < numThreads; i++) {
        threads.submit(new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                LOG.debug("Thread start {}", Thread.currentThread().getName());
                for (int i = 0; i < iterations; i++) {

                    String taskId = UUID.randomUUID().toString();

                    // simulate task processing, some have logs, some don't, processing time varies between each step
                    QSTaskModel task = new QSTaskModel();
                    task.taskId = taskId;
                    Thread.sleep(RandomUtils.nextInt(processingJitterMaxMs));

                    sampledLogService.startedTask(task);
                    Thread.sleep(RandomUtils.nextInt(processingJitterMaxMs));

                    // random number of associated logs [0, 2]
                    for (int j = RandomUtils.nextInt(2); j > 0; j--) {
                        QSLogModel log = new QSLogModel();
                        log.taskId = taskId;
                        sampledLogService.log(log);
                        Thread.sleep(RandomUtils.nextInt(processingJitterMaxMs));
                    }

                    sampledLogService.completedTask(task);
                }
                LOG.debug("Thread end {}", Thread.currentThread().getName());
                return null;

            }
        });
    }
    threads.shutdown();
    threads.awaitTermination(1, TimeUnit.MINUTES);
    long endNs = System.nanoTime();

    // Theoretical maximum number of sampled in task logging
    long durationMs = TimeUnit.NANOSECONDS.toMillis(endNs - startNs);
    long expectedMax = durationMs / noSoonerThanMs + 1; // +1 for time@0: sampled in
    LOG.debug("Run duration: {}ms  no sooner than: {}ms", durationMs, noSoonerThanMs);
    LOG.debug("Expected max sampled in: {}  Actually sampled: {}", expectedMax, sampledTaskIds.size());
    Assert.assertTrue(expectedMax >= sampledTaskIds.size());
}

From source file:io.github.mmichaelis.selenium.client.provider.AbstractWebDriverProviderTest.java

private WebDriver getDriverFromThread(final WebDriverProvider driverProvider)
        throws InterruptedException, ExecutionException {
    final Callable<WebDriver> driverCallable = new WebDriverCallable(driverProvider);
    final ExecutorService executor = newCachedThreadPool();
    final Future<WebDriver> result = executor.submit(driverCallable);
    return result.get();
}

From source file:com.netflix.curator.framework.recipes.atomic.TestDistributedAtomicLong.java

@Test
public void testForceSet() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    client.start();/*from  w w w .jav  a 2s  .co m*/
    try {
        final DistributedAtomicLong dal = new DistributedAtomicLong(client, "/counter", new RetryOneTime(1));

        ExecutorService executorService = Executors.newFixedThreadPool(2);
        executorService.submit(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                for (int i = 0; i < 1000; ++i) {
                    dal.increment();
                    Thread.sleep(10);
                }
                return null;
            }
        });
        executorService.submit(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                for (int i = 0; i < 1000; ++i) {
                    dal.forceSet(0L);
                    Thread.sleep(10);
                }
                return null;
            }
        });

        Assert.assertTrue(dal.get().preValue() < 10);
    } finally {
        client.close();
    }
}

From source file:com.espertech.esper.multithread.TestMTStmtNamedWindowSubqueryAgg.java

private void trySend(int numThreads, int numEventsPerThread, boolean indexShare) throws Exception {
    Configuration config = SupportConfigFactory.getConfiguration();
    config.addEventType("SupportBean", SupportBean.class);
    config.addPlugInAggregationFunction("intListAgg", MyIntListAggregation.class.getName());
    config.getEngineDefaults().getEventMeta()
            .setDefaultEventRepresentation(Configuration.EventRepresentation.MAP); // use Map-type events for testing
    engine = EPServiceProviderManager.getDefaultProvider(config);
    engine.initialize();/*  w ww.j  a v  a  2  s.  co m*/

    // setup statements
    engine.getEPAdministrator().createEPL("create schema UpdateEvent as (uekey string, ueint int)");
    engine.getEPAdministrator().createEPL("create schema WindowSchema as (wskey string, wsint int)");

    String createEpl = "create window MyWindow.win:keepall() as WindowSchema";
    if (indexShare) {
        createEpl = "@Hint('enable_window_subquery_indexshare') " + createEpl;
    }
    EPStatement namedWindow = engine.getEPAdministrator().createEPL(createEpl);

    engine.getEPAdministrator().createEPL("create index ABC on MyWindow(wskey)");
    engine.getEPAdministrator()
            .createEPL("on UpdateEvent mue merge MyWindow mw " + "where uekey = wskey and ueint = wsint "
                    + "when not matched then insert select uekey as wskey, ueint as wsint "
                    + "when matched then delete");
    // note: here all threads use the same string key to insert/delete and different values for the int
    EPStatement targetStatement = engine.getEPAdministrator().createEPL(
            "select (select intListAgg(wsint) from MyWindow mw where wskey = sb.theString) as val from SupportBean sb");

    // execute
    ExecutorService threadPool = Executors.newFixedThreadPool(numThreads);
    Future<Boolean> future[] = new Future[numThreads];
    for (int i = 0; i < numThreads; i++) {
        future[i] = threadPool
                .submit(new StmtNamedWindowSubqueryAggCallable(i, engine, numEventsPerThread, targetStatement));
    }

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

    // total up result
    for (int i = 0; i < numThreads; i++) {
        Boolean result = future[i].get();
        assertTrue(result);
    }

    EventBean[] events = EPAssertionUtil.iteratorToArray(namedWindow.iterator());
    assertEquals(0, events.length);
}

From source file:com.bbytes.jfilesync.sync.ftp.FTPClientFactory.java

/**
 * Get {@link FTPClient} with initialized connects to server given in properties file
 * @return/*from w  ww. j  a  v a2 s .  c o m*/
 */
public FTPClient getClientInstance() {

    ExecutorService ftpclientConnThreadPool = Executors.newSingleThreadExecutor();
    Future<FTPClient> future = ftpclientConnThreadPool.submit(new Callable<FTPClient>() {

        FTPClient ftpClient = new FTPClient();

        boolean connected;

        public FTPClient call() throws Exception {

            try {
                while (!connected) {
                    try {
                        ftpClient.connect(host, port);
                        if (!ftpClient.login(username, password)) {
                            ftpClient.logout();
                        }
                        connected = true;
                        return ftpClient;
                    } catch (Exception e) {
                        connected = false;
                    }

                }

                int reply = ftpClient.getReplyCode();
                // FTPReply stores a set of constants for FTP reply codes.
                if (!FTPReply.isPositiveCompletion(reply)) {
                    ftpClient.disconnect();
                }

                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
            return ftpClient;
        }
    });

    FTPClient ftpClient = new FTPClient();
    try {
        // wait for 100 secs for acquiring conn else terminate
        ftpClient = future.get(100, TimeUnit.SECONDS);
    } catch (TimeoutException e) {
        log.info("FTP client Conn wait thread terminated!");
    } catch (InterruptedException e) {
        log.error(e.getMessage(), e);
    } catch (ExecutionException e) {
        log.error(e.getMessage(), e);
    }

    ftpclientConnThreadPool.shutdownNow();
    return ftpClient;

}

From source file:com.wenyu.clustertools.ClearSnapshot.java

@Override
    public void execute() {
        ExecutorService executor = Executors.newFixedThreadPool(parallel);

        Map<Node, Future<Void>> futures = new HashMap<>();
        for (ClusterToolCmd.Node node : nodes) {
            futures.put(node, executor.submit(new Executor(node)));
        }/* w ww .ja v a2 s.c o  m*/

        for (Map.Entry<ClusterToolCmd.Node, Future<Void>> future : futures.entrySet()) {
            try {
                future.getValue().get(Constants.MAX_PARALLEL_WAIT_IN_SEC, TimeUnit.SECONDS);
            } catch (Exception ex) {
                System.out
                        .println(String.format("%s failed with error: %s", future.getKey().server, ex.toString()));
                ex.printStackTrace();
            }
        }
    }

From source file:com.sinosoft.one.mvc.web.portal.impl.GenericWindowContainer.java

@SuppressWarnings({ "unchecked" })
protected WindowFuture<?> submitWindow(ExecutorService executor, WindowTask task) {
    Future<?> future = executor.submit(task);
    return new WindowFuture(future, task.getWindow());
}

From source file:com.ctrip.infosec.rule.executor.ModelRulesExecutorService.java

@PostConstruct
public void dequeue() {
    int threads = 1;
    ExecutorService executor = Executors.newFixedThreadPool(threads);
    for (int i = 0; i < threads; i++) {
        executor.submit(new Runnable() {
            @Override/*from w  ww  . ja  va  2s  . com*/
            public void run() {
                while (true) {
                    RiskFact fact = null;
                    beforeInvoke("ModelRules.execute");
                    try {
                        fact = queue.take();
                        TraceLogger.beginTrans(fact.eventId);
                        TraceLogger.setParentTransId(
                                valueAsString(fact.ext, Constants.key_traceLoggerParentTransId));
                        fact.ext.remove(Constants.key_traceLoggerParentTransId);
                        execute(fact);
                    } catch (Exception ex) {
                        fault("ModelRules.execute");
                        logger.error("dequeue exception.", ex);
                    } finally {
                        afterInvoke("ModelRules.execute");
                        if (fact != null) {
                            TraceLogger.commitTrans();
                        }
                        Threads.sleep(10, TimeUnit.MILLISECONDS);
                    }
                }
            }
        });
    }
    ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    scheduler.scheduleWithFixedDelay(new Runnable() {
        @Override
        public void run() {
            int size = queue.size();
            if (size > 0) {
                SarsMqStatRepository.put("MODEL_EXECUTE_QUEUE", size);
                logger.warn("queue size: " + size);
            }
            int max = 10000;
            if (size > max) {
                do {
                    RiskFact fact = queue.poll();
                    logger.warn("model queue is full. drop message: " + fact.eventId);
                } while (queue.size() > max);
            }
        }
    }, 30, 30, TimeUnit.SECONDS);
}

From source file:idc.storyalbum.fetcher.FilterService.java

public Set<Photo> filter(Set<Photo> photos, Set<String> tags)
        throws ExecutionException, InterruptedException, FlickrException {
    log.info("Removing from {} photos photos without tags {}", photos.size(), tags);
    ExecutorService executorService = Executors.newFixedThreadPool(20);
    List<Future<Pair<Photo, Photo>>> futures = new ArrayList<>();
    for (Photo photo : photos) {
        futures.add(executorService.submit(new FilterTagTask(photo, tags)));
    }/*from  w ww  .  j  av  a  2  s  .com*/
    executorService.shutdown();
    Set<Photo> result = new HashSet<>();
    for (Future<Pair<Photo, Photo>> future : futures) {
        Pair<Photo, Photo> photoBooleanPair = future.get();
        Photo photo = photoBooleanPair.getLeft();
        String url;
        try {
            url = photo.getOriginalUrl();
        } catch (Exception e) {
            url = photo.getUrl();
        }
        Photo detailedPhoto = photoBooleanPair.getRight();
        if (detailedPhoto == null) {
            log.info("Filtered {}", url);
            photos.remove(photo);
        } else {
            result.add(detailedPhoto);
        }
    }
    return result;

}

From source file:org.green.code.async.executor.ThreadPoolTaskExecutor.java

public Future<?> submit(Runnable task) {
    ExecutorService executor = getThreadPoolExecutor();
    try {/*from w  w  w .  ja  v a  2s  .  com*/
        return executor.submit(task);
    } catch (RejectedExecutionException ex) {
        throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
    }
}