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.wenyu.clustertools.Snapshot.java

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

    Map<ClusterToolCmd.Node, Future<String>> futures = new HashMap<>();
    for (ClusterToolCmd.Node node : nodes) {
        futures.put(node, executor.submit(new Executor(node)));
    }/*w ww.  j  av a2s  . c om*/

    for (Map.Entry<ClusterToolCmd.Node, Future<String>> future : futures.entrySet()) {
        try {
            System.out.println(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.blacklocus.jres.request.index.JresUpdateDocumentScriptTest.java

@Test
public void testRetryOnConflict() throws InterruptedException {
    final String index = "JresUpdateDocumentScriptTest.testRetryOnConflict".toLowerCase();
    final String type = "test";
    final String id = "warzone";

    final AtomicInteger total = new AtomicInteger();
    final AtomicReference<String> error = new AtomicReference<String>();
    final Random random = new Random(System.currentTimeMillis());

    final int numThreads = 16, numIterations = 100;

    ExecutorService x = Executors.newFixedThreadPool(numThreads);
    for (int i = 0; i < numThreads; i++) {
        x.submit(new Runnable() {
            @Override/*from   w  ww. ja v a2 s .co  m*/
            public void run() {
                try {
                    for (int j = 0; j < numIterations; j++) {
                        int increment = random.nextInt(5);
                        total.addAndGet(increment);
                        JresUpdateDocumentScript req = new JresUpdateDocumentScript(index, type, id,
                                "ctx._source.value += increment", ImmutableMap.of("increment", increment),
                                ImmutableMap.of("value", increment), null);
                        req.setRetryOnConflict(numIterations * 10);
                        jres.quest(req);
                    }
                } catch (Exception e) {
                    error.set(e.getMessage());
                }
            }
        });
    }
    x.shutdown();
    x.awaitTermination(1, TimeUnit.MINUTES);

    Assert.assertNull("With so many retries, all of these should have gotten through without conflict error",
            error.get());
    jres.quest(new JresRefresh(index));
    JresGetDocumentReply getReply = jres.quest(new JresGetDocument(index, type, id));
    Map<String, Integer> doc = getReply.getSourceAsType(new TypeReference<Map<String, Integer>>() {
    });
    Assert.assertEquals("All increments should have gotten committed", (Object) total.get(), doc.get("value"));
    Assert.assertEquals("Should have been numThreads * numIterations versions committed",
            (Object) (numThreads * numIterations), getReply.getVersion());
}

From source file:com.netflix.curator.framework.recipes.barriers.TestDistributedDoubleBarrier.java

@Test
public void testMultiClient() throws Exception {
    final Timing timing = new Timing();
    final CountDownLatch postEnterLatch = new CountDownLatch(QTY);
    final CountDownLatch postLeaveLatch = new CountDownLatch(QTY);
    final AtomicInteger count = new AtomicInteger(0);
    final AtomicInteger max = new AtomicInteger(0);
    List<Future<Void>> futures = Lists.newArrayList();
    ExecutorService service = Executors.newCachedThreadPool();
    for (int i = 0; i < QTY; ++i) {
        Future<Void> future = service.submit(new Callable<Void>() {
            @Override//from ww w  .j av  a  2s.co  m
            public Void call() throws Exception {
                CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(),
                        timing.session(), timing.connection(), new RetryOneTime(1));
                try {
                    client.start();
                    DistributedDoubleBarrier barrier = new DistributedDoubleBarrier(client, "/barrier", QTY);

                    Assert.assertTrue(barrier.enter(timing.seconds(), TimeUnit.SECONDS));

                    synchronized (TestDistributedDoubleBarrier.this) {
                        int thisCount = count.incrementAndGet();
                        if (thisCount > max.get()) {
                            max.set(thisCount);
                        }
                    }

                    postEnterLatch.countDown();
                    Assert.assertTrue(timing.awaitLatch(postEnterLatch));

                    Assert.assertEquals(count.get(), QTY);

                    Assert.assertTrue(barrier.leave(timing.seconds(), TimeUnit.SECONDS));
                    count.decrementAndGet();

                    postLeaveLatch.countDown();
                    Assert.assertTrue(timing.awaitLatch(postEnterLatch));
                } finally {
                    IOUtils.closeQuietly(client);
                }

                return null;
            }
        });
        futures.add(future);
    }

    for (Future<Void> f : futures) {
        f.get();
    }
    Assert.assertEquals(count.get(), 0);
    Assert.assertEquals(max.get(), QTY);
}

From source file:com.sixt.service.framework.kafka.messaging.KafkaFailoverIntegrationTest.java

@Test
public void manualKafkaTest() throws InterruptedException {

    ServiceProperties serviceProperties = fillServiceProperties();

    // Topics are created with 3 partitions - see docker-compose-kafkafailover-integrationtest.yml
    Topic ping = new Topic("ping");
    Topic pong = new Topic("pong");

    AtomicInteger sentMessages = new AtomicInteger(0);
    AtomicInteger sendFailures = new AtomicInteger(0);
    AtomicInteger recievedMessages = new AtomicInteger(0);

    Producer producer = new ProducerFactory(serviceProperties).createProducer();

    final AtomicBoolean produceMessages = new AtomicBoolean(true);

    // Produce messages until test tells producer to stop.
    ExecutorService producerExecutor = Executors.newSingleThreadExecutor();
    producerExecutor.submit(new Runnable() {
        @Override/*from   w w  w.ja  v a  2 s. c o  m*/
        public void run() {
            OrangeContext context = new OrangeContext();
            Sleeper sleeper = new Sleeper();

            while (produceMessages.get()) {
                try {

                    String key = RandomStringUtils.randomAscii(5);
                    SayHelloToCmd payload = SayHelloToCmd.newBuilder().setName(key).build();

                    Message request = Messages.requestFor(ping, pong, key, payload, context);
                    producer.send(request);
                    sentMessages.incrementAndGet();

                    sleeper.sleepNoException(1000);
                } catch (Throwable t) {
                    sendFailures.incrementAndGet();
                    logger.error("Caught exception in producer loop", t);
                }
            }
        }
    });

    Consumer consumer = consumerFactoryWithHandler(serviceProperties, SayHelloToCmd.class,
            new MessageHandler<SayHelloToCmd>() {
                @Override
                public void onMessage(Message<SayHelloToCmd> message, OrangeContext context) {
                    recievedMessages.incrementAndGet();
                }
            }).consumerForTopic(ping, new DiscardFailedMessages());

    // Wait to allow manual fiddling with Kafka. Sync with global test timeout above.
    Thread.sleep(2 * 60 * 1000);

    produceMessages.set(false);
    producer.shutdown();

    Thread.sleep(10_000);

    consumer.shutdown();

    logger.info("sentMessages: " + sentMessages.get());
    logger.info("sendFailures: " + sendFailures.get());
    logger.info("recievedMessages: " + recievedMessages.get());
}

From source file:jenkins.plugins.elanceodesk.workplace.notifier.HttpWorkerTest.java

@Test
public void testMutipleTriesWorker() throws InterruptedException {
    ExecutorService executorService = Executors.newCachedThreadPool();
    HttpWorker worker = new HttpWorker("http://localhost:8000/retry-test", "test1body", 30000, retries,
            Mockito.mock(PrintStream.class));
    executorService.submit(worker);
    executorService.shutdown();//from w ww.ja  v a  2s  . c  o  m
    executorService.awaitTermination(5, TimeUnit.SECONDS);
    Assert.assertTrue(MyHandler.getRetryTestResult());
}

From source file:net.openhft.chronicle.logger.log4j1.Log4j1IndexedChroniclePerfTest.java

@Test
public void testMultiThreadLogging() throws IOException, InterruptedException {
    warmup(LoggerFactory.getLogger("perf-binary-indexed-chronicle"));
    warmup(LoggerFactory.getLogger("perf-plain-indexed"));

    final int RUNS = 1000000;
    final int THREADS = 10;

    for (int size : new int[] { 64, 128, 256 }) {
        {/*from w  w w .  j a  v  a  2  s. c om*/
            final long start = System.nanoTime();

            ExecutorService es = Executors.newFixedThreadPool(THREADS);
            for (int t = 0; t < THREADS; t++) {
                es.submit(new RunnableLogger(RUNS, size, "perf-binary-indexed-chronicle"));
            }

            es.shutdown();
            es.awaitTermination(60, TimeUnit.SECONDS);

            final long time = System.nanoTime() - start;

            System.out.printf(
                    "ChronicleLog.MT (runs=%d, min size=%03d, elapsed=%.3f ms) took an average of %.3f us per entry\n",
                    RUNS, size, time / 1e6, time / 1e3 / (RUNS * THREADS));
        }

        {
            final long start = System.nanoTime();

            ExecutorService es = Executors.newFixedThreadPool(THREADS);
            for (int t = 0; t < THREADS; t++) {
                es.submit(new RunnableLogger(RUNS, size, "perf-plain-indexed"));
            }

            es.shutdown();
            es.awaitTermination(60, TimeUnit.SECONDS);

            final long time = System.nanoTime() - start;

            System.out.printf(
                    "Plain.MT (runs=%d, min size=%03d, elapsed=%.3f ms)): took an average of %.3f us per entry\n",
                    RUNS, size, time / 1e6, time / 1e3 / (RUNS * THREADS));
        }
    }

    ChronicleTools.deleteOnExit(basePath("perf-binary-indexed-chronicle"));
}

From source file:net.openhft.chronicle.logger.log4j1.Log4j1VanillaChroniclePerfTest.java

@Test
public void testMultiThreadLogging() throws IOException, InterruptedException {
    warmup(LoggerFactory.getLogger("perf-binary-vanilla-chronicle"));
    warmup(LoggerFactory.getLogger("perf-plain-vanilla"));

    final int RUNS = 100000; // ~ 10s
    final int THREADS = Runtime.getRuntime().availableProcessors();

    for (int size : new int[] { 64, 128, 256 }) {
        {// w  w  w  . j  a v a 2s.  co m
            final long start = System.nanoTime();

            ExecutorService es = Executors.newFixedThreadPool(THREADS);
            for (int t = 0; t < THREADS; t++) {
                es.submit(new RunnableLogger(RUNS, size, "perf-binary-vanilla-chronicle"));
            }

            es.shutdown();
            es.awaitTermination(2, TimeUnit.MINUTES);

            final long time = System.nanoTime() - start;

            System.out.printf(
                    "ChronicleLog.MT (runs=%d, min size=%03d, elapsed=%.3f ms) took an average of %.3f us per entry\n",
                    RUNS, size, time / 1e6, time / 1e3 / (RUNS * THREADS));
        }

        {
            final long start = System.nanoTime();

            ExecutorService es = Executors.newFixedThreadPool(THREADS);
            for (int t = 0; t < THREADS; t++) {
                es.submit(new RunnableLogger(RUNS, size, "perf-plain-vanilla"));
            }

            es.shutdown();
            es.awaitTermination(5, TimeUnit.MINUTES);

            final long time = System.nanoTime() - start;

            System.out.printf(
                    "Plain.MT (runs=%d, min size=%03d, elapsed=%.3f ms)): took an average of %.3f us per entry\n",
                    RUNS, size, time / 1e6, time / 1e3 / (RUNS * THREADS));
        }
    }

    IOTools.deleteDir(basePath("perf-binary-vanilla-chronicle"));
}

From source file:io.undertow.servlet.test.request.ExecutorPerServletTestCase.java

public int runTest(final String path) throws IOException, ExecutionException, InterruptedException {
    ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
    try {/*from   w ww.  ja  v a 2 s.c om*/

        final List<Future<?>> futures = new ArrayList<>();
        for (int i = 0; i < NUM_THREADS; ++i) {
            futures.add(executor.submit(new Runnable() {
                @Override
                public void run() {
                    TestHttpClient client = new TestHttpClient();
                    try {
                        for (int i = 0; i < NUM_REQUESTS; ++i) {
                            HttpGet get = new HttpGet(
                                    DefaultServer.getDefaultServerURL() + "/servletContext" + path);
                            HttpResponse result = client.execute(get);
                            Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
                            final String response = HttpClientUtils.readResponse(result);
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    } finally {
                        client.getConnectionManager().shutdown();
                    }
                }
            }));
        }
        for (Future<?> future : futures) {
            future.get();
        }
        TestHttpClient client = new TestHttpClient();
        try {
            HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext" + path);
            HttpResponse result = client.execute(get);
            Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
            return Integer.parseInt(HttpClientUtils.readResponse(result));
        } finally {
            client.getConnectionManager().shutdown();
        }
    } finally {
        executor.shutdown();
    }
}

From source file:net.openhft.chronicle.logger.logback.LogbackVanillaChroniclePerfTest.java

@Test
public void testMultiThreadLogging() throws IOException, InterruptedException {
    warmup(LoggerFactory.getLogger("perf-binary-vanilla-chronicle"));
    warmup(LoggerFactory.getLogger("perf-plain-vanilla"));

    final int THREADS = Runtime.getRuntime().availableProcessors();

    for (int size : new int[] { 64, 128, 256 }) {
        {/* w w  w .j  a v a2s.c om*/
            final int RUNS = 250000;
            final long start = System.nanoTime();

            ExecutorService es = Executors.newFixedThreadPool(THREADS);
            for (int t = 0; t < THREADS; t++) {
                es.submit(new RunnableLogger(RUNS, size, "perf-binary-vanilla-chronicle"));
            }

            es.shutdown();
            es.awaitTermination(2, TimeUnit.MINUTES);

            final long time = System.nanoTime() - start;

            System.out.printf(
                    "ChronicleLog.MT (runs=%d, min size=%03d, elapsed=%.3f ms) took an average of %.3f us per entry\n",
                    RUNS, size, time / 1e6, time / 1e3 / (RUNS * THREADS));
        }

        {
            final int RUNS = 10000;
            final long start = System.nanoTime();

            ExecutorService es = Executors.newFixedThreadPool(THREADS);
            for (int t = 0; t < THREADS; t++) {
                es.submit(new RunnableLogger(RUNS, size, "perf-plain-vanilla"));
            }

            es.shutdown();
            es.awaitTermination(5, TimeUnit.MINUTES);

            final long time = System.nanoTime() - start;

            System.out.printf(
                    "Plain.MT (runs=%d, min size=%03d, elapsed=%.3f ms)): took an average of %.3f us per entry\n",
                    RUNS, size, time / 1e6, time / 1e3 / (RUNS * THREADS));
        }
    }

    IOTools.deleteDir(basePath("perf-binary-vanilla-chronicle"));
}

From source file:net.openhft.chronicle.logger.log4j2.Log4j2VanillaChroniclePerfTest.java

@Test
public void testMultiThreadLogging() throws IOException, InterruptedException {

    warmup(LoggerFactory.getLogger("perf-binary-vanilla-chronicle"));
    warmup(LoggerFactory.getLogger("perf-plain-vanilla"));

    final int RUNS = 250000; // ~10s
    final int THREADS = Runtime.getRuntime().availableProcessors();

    for (int size : new int[] { 64, 128, 256 }) {
        {//  w ww.j  a  v  a  2  s .c  o  m
            final long start = System.nanoTime();

            ExecutorService es = Executors.newFixedThreadPool(THREADS);
            for (int t = 0; t < THREADS; t++) {
                es.submit(new RunnableLogger(RUNS, size, "perf-binary-vanilla-chronicle"));
            }

            es.shutdown();
            es.awaitTermination(2, TimeUnit.MINUTES);

            final long time = System.nanoTime() - start;

            System.out.printf(
                    "ChronicleLog.MT (runs=%d, min size=%03d, elapsed=%.3f ms) took an average of %.3f us per entry\n",
                    RUNS, size, time / 1e6, time / 1e3 / (RUNS * THREADS));
        }

        {
            final long start = System.nanoTime();

            ExecutorService es = Executors.newFixedThreadPool(THREADS);
            for (int t = 0; t < THREADS; t++) {
                es.submit(new RunnableLogger(RUNS, size, "perf-plain-vanilla"));
            }

            es.shutdown();
            es.awaitTermination(10, TimeUnit.MINUTES);

            final long time = System.nanoTime() - start;

            System.out.printf(
                    "Plain.MT (runs=%d, min size=%03d, elapsed=%.3f ms)): took an average of %.3f us per entry\n",
                    RUNS, size, time / 1e6, time / 1e3 / (RUNS * THREADS));
        }
    }

    IOTools.deleteDir(basePath("perf-binary-vanilla-chronicle"));
}