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:org.jolokia.client.request.J4pConnectionPoolingIntegrationTest.java

private void searchParallel(J4pClient j4pClient) throws Exception {
    stubFor(get(urlPathMatching("/test/([a-z]*)"))
            .willReturn(aResponse().withFixedDelay(1000).withBody(getJsonResponse("test"))));

    final ExecutorService executorService = Executors.newFixedThreadPool(20);
    final J4pSearchRequest j4pSearchRequest = new J4pSearchRequest("java.lang:type=*");

    final List<Future<Void>> requestsList = new ArrayList<Future<Void>>();

    for (int i = 0; i < 20; i++) {
        requestsList.add(executorService.submit(new AsyncRequest(j4pClient, j4pSearchRequest)));
    }//  w  w  w .  j  av  a  2s  .c o  m

    for (Future<Void> requests : requestsList) {
        requests.get();
    }

    executorService.shutdown();
}

From source file:com.playonlinux.filesystem.DirectoryWatcher.java

public DirectoryWatcher(ExecutorService executorService, Path observedDirectory) {
    try {/*from   ww w  .  j  av a2 s.com*/
        validate(observedDirectory);
        this.observedDirectory = observedDirectory;
        this.watcher = FileSystems.getDefault().newWatchService();

        observedDirectory.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
        executorService.submit(this::run);
    } catch (IOException e) {
        throw new RuntimeException(
                String.format("Unable to create watcher for %s", observedDirectory.toString()));
    }
}

From source file:fr.inria.wimmics.coresetimer.CoreseTimer.java

public CoreseTimer run() throws ClassNotFoundException, IllegalAccessException, InstantiationException,
        IOException, LoadException {
    LOGGER.entering(CoreseTimer.class.getName(), "run");
    assert (initialized);

    // Loading the nq data in corese, then applying several times the query.
    LOGGER.log(Level.INFO, "beginning with input #{0}", test.getInput());
    // require to have a brand new adapter for each new input set.
    adapter = (CoreseAdapter) Class.forName(adapterName).newInstance();

    String inputFileName = "";
    switch (mode) {
    case MEMORY: {
        inputFileName += test.getInput();
        adapter.preProcessing(inputFileName, true);
        break;//w  ww . j  a v  a2  s  . co m
    }
    case DB: {
        inputFileName += test.getInputDb();
        System.setProperty("fr.inria.corese.tinkerpop.dbinput", inputFileName);
        adapter.preProcessing(inputFileName, false);
        break;
    }
    }

    String query = test.getRequest();
    LOGGER.log(Level.INFO, "processing nbQuery #{0}", query);
    stats = new DescriptiveStatistics();
    statsMemory = new DescriptiveStatistics();
    int nbCycles = test.getMeasuredCycles() + test.getWarmupCycles();
    boolean measured = true;
    for (int i = 0; i < nbCycles; i++) {
        LOGGER.log(Level.INFO, "iteration #{0}", i);
        System.gc();
        final long startTime = System.currentTimeMillis();
        LOGGER.log(Level.INFO, "before query");

        ExecutorService executor = Executors.newSingleThreadExecutor();
        Future<?> future = executor.submit(new Runnable() {
            @Override
            public void run() {
                adapter.execQuery(query);
            }
        });

        try {
            future.get(1, TimeUnit.HOURS);
            measured = true;
        } catch (InterruptedException | TimeoutException e) {
            future.cancel(true);
            measured = false;
            LOGGER.log(Level.WARNING, "Terminated!");
        } catch (ExecutionException ex) {
            Logger.getLogger(CoreseTimer.class.getName()).log(Level.SEVERE, null, ex);
        }
        executor.shutdownNow();

        LOGGER.log(Level.INFO, "after query");
        final long endTime = System.currentTimeMillis();
        long delta = endTime - startTime;
        long memoryUsage = getMemoryUsage();
        LOGGER.info(String.format("elapsed time = %d ms", delta));
        LOGGER.info(String.format("used memory = %d bytes", memoryUsage));
        if (i >= test.getWarmupCycles()) {
            if (!measured) {
                while (i < nbCycles) {
                    stats.addValue(-100);
                    statsMemory.addValue(memoryUsage);
                    i++;
                }
            } else {
                stats.addValue(delta);
                statsMemory.addValue(memoryUsage);
            }
        }
    }
    adapter.saveResults(test.getOutputPath());
    mappings = adapter.getMappings();
    adapter.postProcessing();
    LOGGER.exiting(CoreseTimer.class.getName(), "run");
    return this;
}

From source file:io.undertow.server.handlers.RequestLimitingHandlerTestCase.java

@Test
public void testRateLimitingHandlerQueueFull() throws ExecutionException, InterruptedException {
    latch.countDown();//from  w w  w.j a  va 2  s.  c  om
    latch = new CountDownLatch(1);
    ExecutorService executor = Executors.newFixedThreadPool(N_THREADS * 2);
    try {
        final List<Future<?>> futures = new ArrayList<>();
        for (int i = 0; i < N_THREADS * 2; ++i) {
            futures.add(executor.submit(new Callable<String>() {
                @Override
                public String call() {
                    TestHttpClient client = new TestHttpClient();
                    try {
                        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL());
                        HttpResponse result = client.execute(get);
                        if (result.getStatusLine().getStatusCode() == 513) {
                            return "513";
                        }
                        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
                        return HttpClientUtils.readResponse(result);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    } finally {
                        client.getConnectionManager().shutdown();
                    }
                }
            }));
        }
        Thread.sleep(300);
        latch.countDown();
        for (Future<?> future : futures) {
            String res = (String) future.get();
            Assert.assertTrue(res, res.equals("1") || res.equals("2") || res.equals("513"));
        }
        futures.clear();
        for (int i = 0; i < 2; ++i) {
            futures.add(executor.submit(new Callable<String>() {
                @Override
                public String call() {
                    TestHttpClient client = new TestHttpClient();
                    try {
                        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL());
                        HttpResponse result = client.execute(get);
                        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
                        return HttpClientUtils.readResponse(result);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    } finally {
                        client.getConnectionManager().shutdown();
                    }
                }
            }));
        }

        for (Future<?> future : futures) {
            String res = (String) future.get();
            Assert.assertTrue(res, res.equals("1") || res.equals("2"));
        }

    } finally {
        executor.shutdown();
    }

}

From source file:com.clonephpscrapper.crawler.ClonePhpScrapper.java

public void crawledCategories() throws URISyntaxException, IOException, InterruptedException, Exception {

    String url = "http://clonephp.com/";

    //       Document doc = Jsoup.parse(fetchPage(new URI(url)));
    String response = "";
    response = new GetRequestHandler().doGetRequest(new URL(url));

    Document doc = Jsoup.parse(response);

    Elements ele = doc.select("table[class=dir] tbody tr td table[class=dir_cat] tbody tr th a");//.first();

    for (Element ele1 : ele) {
        objCategories = new Categories();

        String categoryName = ele1.text();
        String categoryUrl = "http://clonephp.com/" + ele1.attr("href");

        System.out.println("CATEGORY_NAME : " + categoryName);
        System.out.println("CATEGORY_URL  : " + categoryUrl);

        objCategories.setCategoryName(categoryName);
        objCategories.setCategoryUrl(categoryUrl);

        objClonePhpDaoImpl.insertCategoriesData(objCategories);

        //            objCrawlingEachUrlData.crawlingUrlData(categoryUrl);
    }//  w ww .  j  a v  a  2 s  . co  m

    List<Future<String>> list = new ArrayList<Future<String>>();
    ExecutorService executor = Executors.newFixedThreadPool(5);

    List<Categories> listCatogories = objClonePhpDaoImpl.getCategoriesDataList();

    for (Categories listCatogory : listCatogories) {

        try {
            Callable worker = new CrawlingEachUrlData(listCatogory, objClonePhpDaoImpl);
            Future<String> future = executor.submit(worker);
            list.add(future);
        } catch (Exception exx) {
            System.out.println(exx);
        }

    }

    for (Future<String> fut : list) {
        try {
            //print the return value of Future, notice the output delay in console
            // because Future.get() waits for task to get completed
            System.out.println(new Date() + "::" + fut.get());
        } catch (InterruptedException | ExecutionException ep) {
            ep.printStackTrace();
        }
    }
    //shut down the executor service now
    executor.shutdown();

}

From source file:com.seajas.search.attender.service.task.SubscriberSenderTask.java

/**
 * Send out the actual profile e-mails.//from  ww w .  j  a va 2s. c  o m
 */
public void send() {
    if (Boolean.getBoolean("attender.attending.disabled")) {
        logger.info(
                "Attending has been explicitly disabled using 'attender.attending.disabled=true'. Skipping subscriber checks.");

        return;
    }

    logger.info("Started subscriber job");

    // Create a new UTC-based calendar to base ourselves on

    final Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    final Date utcDate = calendar.getTime();

    // Also use a regular date for reference, to pass to the search engine

    final Date currentDate = new Date();

    // Retrieve the list of profile profiles to attend to

    List<Profile> profiles = attenderService.getNotifiableProfiles(calendar);

    if (profiles.size() == 0) {
        logger.info("No notifiable profiles found - finishing subscriber job");

        return;
    }

    // Re-initialize the mail-server, if needed

    mailService.updateWorkingMailServer();

    // Now process the profiles using a thread-pool of at most threadMaximum threads

    ExecutorService threadPool = Executors.newFixedThreadPool(Math.min(profiles.size(), threadMaximum));

    for (final Profile profile : profiles)
        threadPool.submit(new Runnable() {
            @Override
            public void run() {
                List<ProfileSubscriber> subscribers = attenderService.getNotifiableSubscribers(calendar,
                        profile.getSubscribers());

                for (ProfileSubscriber subscriber : subscribers) {
                    try {
                        // Round up the search parameters and taxonomies into a concise parameter list

                        Map<String, String> searchParameters = InterfaceQueryUtils
                                .combineParametersAndTaxonomies(profile.getSearchParametersMap(),
                                        profile.getTaxonomyIdentifierNumbers());

                        String searchQuery = InterfaceQueryUtils.createQueryFromParameters(profile.getQuery(),
                                searchParameters);

                        // Determine the start date depending on the user's last notification date, and retrieve the results

                        List<SearchResult> searchResults = searchService.performSearch(profile.getQuery(),
                                subscriber.getLastNotification(), currentDate, searchParameters,
                                mailService.getResultsPerMessage(), subscriber.getEmailLanguage());

                        if (logger.isInfoEnabled())
                            logger.info("Found " + searchResults.size() + " result(s) for profile with ID "
                                    + profile.getId() + " and query '" + profile.getQuery() + "'");

                        // Now create a template result based on the search results

                        TemplateResult templateResult = templateService.createResults(
                                subscriber.getEmailLanguage(), searchQuery, profile.getQuery(),
                                subscriber.getUniqueId(), profile.getUniqueId(), searchResults);

                        // Take the subject in the given language, or resort to the default language if it doesn't exist (yet)

                        String subject = null;

                        try {
                            subject = messageSource.getMessage(
                                    "mail.results.subject."
                                            + subscriber.getNotificationType().toString().toLowerCase(),
                                    new Object[] { profile.getQuery() },
                                    new Locale(subscriber.getEmailLanguage()));
                        } catch (NoSuchMessageException e) {
                            logger.warn("Could not retrieve results message subject header in language "
                                    + subscriber.getEmailLanguage() + ", resorting to the default language "
                                    + attenderService.getDefaultApplicationLanguage());

                            subject = messageSource.getMessage("mail.confirmation.subject",
                                    new Object[] { profile.getQuery() },
                                    new Locale(attenderService.getDefaultApplicationLanguage()));
                        }

                        // And send out the actual e-mail

                        if (mailService.hasWorkingMailServer())
                            mailService.sendMessage(subscriber.getEmail(), subject,
                                    templateResult.getTextResult(), templateResult.getHtmlResult());
                        else
                            logger.error(
                                    "Could not e-mail the results message - no mail server has been configured. Users may retrieve results using the RSS feed.");

                        // Update the processed subscribers' lastNotification indicator

                        attenderService.updateProfileSubscriberLastNotification(subscriber.getId(), utcDate);
                    } catch (ScriptException e) {
                        logger.error("Could not create a template result", e);
                    }
                }
            }
        });

    logger.info("Finishing subscriber job");
}

From source file:com.mapr.synth.drive.Trails.java

@Override
public void setup() {
    ExecutorService pool = Executors.newFixedThreadPool(1);
    BlockingQueue<State> q = new ArrayBlockingQueue<>(2000);
    input = q;/*from   w  ww .  java2 s.  com*/
    pool.submit(new Producer(q));
    speedDistribution = new AVLTreeDigest(300);
    noise = new Random();

    speed = new Stripchart(10, 430, 460, 80, 1, 0, 0, 90);
    rpm = new Stripchart(10, 520, 460, 80, 1, 0, 0, 2200);
    throttle = new Stripchart(10, 610, 460, 80, 1, 0, 0, 100);

    frameRate(15);
}

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

private void tryPerformanceDispatch(int numThreads, int numRepeats) throws Exception {
    MyListener listener = new MyListener();
    engine.getEPAdministrator().getStatement("select").addListener(listener);

    List<Object>[] events = new ArrayList[numThreads];
    int eventId = 0;
    for (int threadNum = 0; threadNum < numThreads; threadNum++) {
        events[threadNum] = new ArrayList<Object>();
        for (int eventNum = 0; eventNum < numRepeats; eventNum++) {
            // range: 1 to 1000
            int partition = (int) (Math.random() * 50);
            eventId++;/*from w  ww  . j  a va 2 s.  c  o  m*/
            events[threadNum].add(new SupportBean(new Integer(partition).toString(), eventId));
        }
    }

    ExecutorService threadPool = Executors.newFixedThreadPool(numThreads);
    Future futures[] = new Future[numThreads];
    long startTime = System.currentTimeMillis();

    for (int i = 0; i < numThreads; i++) {
        Callable callable = new SendEventCallable(i, engine, events[i].iterator());
        futures[i] = threadPool.submit(callable);
    }
    for (Future future : futures) {
        assertEquals(true, future.get());
    }
    long delta = System.currentTimeMillis() - startTime;

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

    // print those events not received
    for (List<Object> eventList : events) {
        for (Object event : eventList) {
            if (!listener.getBeans().contains(event)) {
                log.info("Expected event was not received, event " + event);
            }
        }
    }

    assertEquals(numRepeats * numThreads, listener.getBeans().size());
    assertTrue("delta=" + delta, delta < 500);
}

From source file:com.netflix.curator.framework.recipes.leader.TestLeaderLatch.java

private void basic(Mode mode) throws Exception {
    final int PARTICIPANT_QTY = 1;//0;

    List<LeaderLatch> latches = Lists.newArrayList();

    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(),
            timing.connection(), new RetryOneTime(1));
    try {/*from ww w.  j a  v  a2  s . co  m*/
        client.start();

        for (int i = 0; i < PARTICIPANT_QTY; ++i) {
            LeaderLatch latch = new LeaderLatch(client, PATH_NAME);
            if (mode == Mode.START_IMMEDIATELY) {
                latch.start();
            }
            latches.add(latch);
        }
        if (mode == Mode.START_IN_THREADS) {
            ExecutorService service = Executors.newFixedThreadPool(latches.size());
            for (final LeaderLatch latch : latches) {
                service.submit(new Callable<Object>() {
                    @Override
                    public Object call() throws Exception {
                        Thread.sleep((int) (100 * Math.random()));
                        latch.start();
                        return null;
                    }
                });
            }
            service.shutdown();
        }

        while (latches.size() > 0) {
            List<LeaderLatch> leaders = waitForALeader(latches, timing);
            Assert.assertEquals(leaders.size(), 1); // there can only be one leader
            LeaderLatch theLeader = leaders.get(0);
            if (mode == Mode.START_IMMEDIATELY) {
                Assert.assertEquals(latches.indexOf(theLeader), 0); // assert ordering - leadership should advance in start order
            }
            theLeader.close();
            latches.remove(theLeader);
        }
    } finally {
        for (LeaderLatch latch : latches) {
            IOUtils.closeQuietly(latch);
        }
        IOUtils.closeQuietly(client);
    }
}

From source file:com.jivesoftware.os.rcvs.api.keys.SymetricalHashableKeyTest.java

License:asdf

@Test
public void testThreadSafe() throws Exception {
    System.out.println("testThreadSafe");
    final SymetricalHashableKey instance = new SymetricalHashableKey("asdfghjk");
    int poolSize = 4;
    ExecutorService threadPool = Executors.newFixedThreadPool(poolSize);
    final CountDownLatch latch = new CountDownLatch(poolSize);
    final MutableBoolean failed = new MutableBoolean(false);
    for (int i = 0; i < poolSize; i++) {
        threadPool.submit(new Runnable() {
            @Override/*from w ww  . j  a v a 2s  .  com*/
            public void run() {
                try {
                    int count = 1000000;
                    for (long i = 0; i < count; i++) {
                        long a = Math.abs(rand.nextLong());
                        byte[] hash = instance.toHash(longBytes(a));
                        byte[] output = instance.toBytes(hash);
                        long b = bytesLong(output);
                        if (a != b) {
                            System.out.println("Failed a:" + a + " ne b:" + b);
                            failed.setValue(true);
                            return;
                        }
                    }
                } catch (InvalidKeyException | ShortBufferException | IllegalBlockSizeException
                        | BadPaddingException | ExecutionException x) {
                    failed.setValue(true);
                } finally {
                    latch.countDown();
                }
            }
        });
    }

    latch.await();
    Assert.assertFalse(failed.booleanValue());
}