Package java.util.concurrent

Examples of java.util.concurrent.ExecutorService


            }
            // System.out.println(locToSet);
            mStore.createContainerInLocation(locToSet, mContainerName);
        }

        final ExecutorService writerService = Executors.newFixedThreadPool(20);
        final ExecutorService readerService = Executors.newFixedThreadPool(20);
        mRunningWriteTasks = new ConcurrentHashMap<Integer , Future<Integer>>();
        mRunningReadTasks = new ConcurrentHashMap<Integer , Future<Map.Entry<Integer , byte[]>>>();

        mReaderService = new ExecutorCompletionService<Map.Entry<Integer , byte[]>>(readerService);
        final Thread readHashmapCleaner = new Thread(new ReadFutureCleaner());
View Full Code Here


      group.shutdownGracefully();
    }

    {
      final ExecutorService listenerExecutorService = Executors.newSingleThreadExecutor();

      final PushManager<ApnsPushNotification> providedExecutorServicePushManager =
          new PushManager<ApnsPushNotification>(TEST_ENVIRONMENT, SSLTestUtil.createSSLContextForTestClient(),
              null, listenerExecutorService, null, new PushManagerConfiguration(), TEST_PUSH_MANAGER_NAME);

      providedExecutorServicePushManager.start();
      providedExecutorServicePushManager.shutdown();

      assertTrue(providedExecutorServicePushManager.isShutDown());
      assertFalse(listenerExecutorService.isShutdown());

      listenerExecutorService.shutdown();
    }
  }
View Full Code Here

        assertEquals(0.0, simpleCounter.getEventRate(), 0.0);
    }

    @Test
    public void testMultipleThreads() throws InterruptedException {
        ExecutorService executorService = null;
        try {
            final int NUM_THREADS = 5;
            final int NUM_OPS = 10000;

            long startTimeMs = System.currentTimeMillis();
            executorService = Executors.newFixedThreadPool(NUM_THREADS);
            final CountDownLatch latch1 = new CountDownLatch(NUM_THREADS);
            final CountDownLatch latch0 = new CountDownLatch(1);

            for(int i = 0; i < NUM_THREADS; i++) {
                final int threadId = i;
                executorService.submit(new Runnable() {

                    public void run() {
                        try {
                            latch0.await();
                            for(int j = 0; j < NUM_OPS; j++) {
                                simpleCounter.count(100 * (threadId + 1));
                            }
                        } catch(InterruptedException e) {
                            e.printStackTrace();
                        } finally {
                            latch1.countDown();
                        }
                    }
                });
            }
            latch0.countDown();
            latch1.await();
            // one more sleep so we expire the current interval where all the
            // action happened
            sleepForResetInterval(startTimeMs);

            startTimeMs = System.currentTimeMillis();
            assertEquals(300.0, simpleCounter.getAvgEventValue(), 0.0);
            assertEquals((NUM_OPS * NUM_THREADS)
                                 / ((COUNTER_RESET_INTERVAL_MS * 1.0) / Time.MS_PER_SECOND),
                         simpleCounter.getEventRate(),
                         0.0);
            sleepForResetInterval(startTimeMs);

            // Run for a long period spannning multiple intervals and see if we
            // observe if we see consitent metrics
            final ConcurrentLinkedQueue<Double> observedEventRate = new ConcurrentLinkedQueue<Double>();
            final ConcurrentLinkedQueue<Double> observedEventValueAvg = new ConcurrentLinkedQueue<Double>();
            final int NUM_INTERVALS = 30;
            final CountDownLatch latch2 = new CountDownLatch(NUM_THREADS);
            for(int i = 0; i < NUM_THREADS; i++) {
                executorService.submit(new Runnable() {

                    public void run() {
                        try {
                            for(int interval = 0; interval < NUM_INTERVALS; interval++) {
                                long startTimeMs = System.currentTimeMillis();
                                for(int j = 0; j < NUM_OPS; j++) {
                                    simpleCounter.count(100);
                                }
                                sleepForResetInterval(startTimeMs);
                            }
                            observedEventRate.add(simpleCounter.getEventRate());
                            observedEventValueAvg.add(simpleCounter.getAvgEventValue());
                        } finally {
                            latch2.countDown();
                        }
                    }
                });
            }

            latch2.await();
            Object[] actualEventRates = new Object[NUM_THREADS];
            Object[] actualEventValueAvgs = new Object[NUM_THREADS];
            for(int i = 0; i < NUM_THREADS; i++) {
                actualEventRates[i] = (NUM_OPS * NUM_THREADS)
                                      / ((COUNTER_RESET_INTERVAL_MS * 1.0) / Time.MS_PER_SECOND);
                actualEventValueAvgs[i] = 100.0;
            }
            assertEquals(Arrays.equals(observedEventRate.toArray(), actualEventRates), true);
            assertEquals(Arrays.equals(observedEventValueAvg.toArray(), actualEventValueAvgs), true);

        } finally {
            if(executorService != null)
                executorService.shutdown();
        }
    }
View Full Code Here

    @Override
    protected void shutDown() throws Exception {
        LOG.debug("Stopping BufferSynchronizerService");
        if (indexerAvailable && cluster.isConnectedAndHealthy()) {
            final ExecutorService executorService = new InstrumentedExecutorService(Executors.newFixedThreadPool(2), metricRegistry);

            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    bufferSynchronizer.waitForEmptyBuffers(configuration.getShutdownTimeout(), TimeUnit.MILLISECONDS);
                }
            });

            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    cacheSynchronizer.waitForEmptyCaches(configuration.getShutdownTimeout(), TimeUnit.MILLISECONDS);
                }
            });

            executorService.shutdown();
            executorService.awaitTermination(configuration.getShutdownTimeout(), TimeUnit.MILLISECONDS);
        } else {
            LOG.warn("Elasticsearch is unavailable. Not waiting to clear buffers and caches, as we have no healthy cluster.");
        }
        LOG.debug("Stopped BufferSynchronizerService");
    }
View Full Code Here

            tlsCertFile = configuration.getRestTlsCertFile();
            tlsKeyFile = configuration.getRestTlsKeyFile();
        }

        // TODO Magic numbers
        final ExecutorService executor = new InstrumentedExecutorService(new OrderedMemoryAwareThreadPoolExecutor(configuration.getRestThreadPoolSize(), 1048576, 1048576), metricRegistry);

        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            @Override
            public ChannelPipeline getPipeline() throws Exception {
                final ChannelPipeline pipeline = Channels.pipeline();
View Full Code Here

    public synchronized void configure(final int poolSize)
    {
        if ( oldSize != poolSize)
        {
            oldSize = poolSize;
            final ExecutorService oldService = this.executor;
            this.executor = Executors.newFixedThreadPool(poolSize, threadFactory);
            if ( oldService != null )
            {
                oldService.shutdown();
            }
        }
    }
View Full Code Here

        Node node = Iterables.get(failureDetectorConfig.getCluster().getNodes(), 0);
        CountDownLatch countDownLatch = new CountDownLatch(1);
        Listener listener = new Listener(failureDetectorConfig.getTime());
        failureDetector.addFailureDetectorListener(listener);

        ExecutorService threadPool = Executors.newFixedThreadPool(11);

        for(int i = 0; i < 10; i++)
            threadPool.submit(new NodeAccessorRunnable(failureDetector,
                                                       node,
                                                       countDownLatch,
                                                       null,
                                                       null,
                                                       null,
                                                       0,
                                                       10));

        threadPool.submit(new TimedUnavailability(failureDetector, node, countDownLatch));

        threadPool.shutdown();

        // If we get stuck, we should give the user the opportunity to get a
        // thread dump.
        if(!threadPool.awaitTermination(60, TimeUnit.SECONDS)) {
            System.out.println("Threads appear to be stuck");
            threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
        }

        return Class.forName(failureDetectorConfig.getImplementationClassName()).getSimpleName()
               + ", " + listener.getDelta();
    }
View Full Code Here

                               int nThreads,
                               int nRequests) throws Exception {

        final KeyedResourcePool<K, V> pool = new KeyedResourcePool<K, V>(factory, config);
        final ConcurrentHashMap<K, AtomicInteger> resourceInHand = new ConcurrentHashMap<K, AtomicInteger>();
        ExecutorService executor = Executors.newFixedThreadPool(nThreads);
        final TestStats testStats = new TestStats();
        final AtomicBoolean passed = new AtomicBoolean(true);

        for(int i = 0; i < nRequests; i++) {
            final K key = getRequestKey();
            resourceInHand.putIfAbsent(key, new AtomicInteger(0));
            executor.execute(new Runnable() {

                public void run() {
                    try {
                        // borrow resource
                        V resource = pool.checkout(key);
                        resourceInHand.get(key).incrementAndGet();

                        // assert that resourceInHand is less than equal to pool
                        // Size
                        if(resourceInHand.get(key).get() > config.getMaxPoolSize()) {
                            passed.set(false);
                        }

                        assertEquals("resources In Hand(" + resourceInHand.get(key).get()
                                             + ") should be less than equal to pool size("
                                             + config.getMaxPoolSize() + ")",
                                     true,
                                     resourceInHand.get(key).get() <= config.getMaxPoolSize());

                        // do something
                        doSomethingWithResource(key, resource);

                        // return
                        resourceInHand.get(key).decrementAndGet();
                        pool.checkin(key, resource);
                    } catch(TimeoutException e) {
                        ++testStats.timeoutRequests;
                        return;
                    } catch(Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            });
        }

        executor.shutdown();
        executor.awaitTermination(5 * 60, TimeUnit.SECONDS);

        if(!passed.get()) {
            fail();
        }
        return testStats;
View Full Code Here

        final Random rand = new Random();
        int iterations = totalSize / increment;
        long[] readTimes = new long[iterations];
        long[] writeTimes = new long[iterations];

        ExecutorService service = Executors.newFixedThreadPool(threads);
        for(int i = 0; i < iterations; i++) {
            System.out.println("Starting iteration " + i);
            List<Future<Object>> results = new ArrayList<Future<Object>>(increment);
            long startTime = System.currentTimeMillis();
            final int fi = i;
            for(int j = 0; j < increment; j++) {
                final int fj = j;
                results.add(service.submit(new Callable<Object>() {

                    public Object call() throws Exception {
                        db.put(null,
                               new DatabaseEntry(Integer.toString(fi * increment + fj).getBytes()),
                               new DatabaseEntry(Integer.toString(fi * increment + fj).getBytes()));
                        return null;
                    }
                }));
            }
            for(int j = 0; j < increment; j++)
                results.get(j).get();
            writeTimes[i] = System.currentTimeMillis() - startTime;
            System.out.println("write: " + (writeTimes[i] / (double) increment));
            results.clear();

            startTime = System.currentTimeMillis();
            for(int j = 0; j < increment; j++) {
                results.add(service.submit(new Callable<Object>() {

                    public Object call() throws Exception {
                        int value = rand.nextInt((fi + 1) * increment);
                        return db.get(null,
                                      new DatabaseEntry(Integer.toString(value).getBytes()),
View Full Code Here

    protected void execute(Map<String, String> hostNameCommandLineMap)
            throws RemoteOperationException {
        CommandLineParser commandLineParser = new CommandLineParser();

        ExecutorService threadPool = Executors.newFixedThreadPool(hostNameCommandLineMap.size());
        List<Future<?>> futures = new ArrayList<Future<?>>();

        for(Map.Entry<String, String> entry: hostNameCommandLineMap.entrySet()) {
            String hostName = entry.getKey();
            String commandLine = entry.getValue();

            if(logger.isDebugEnabled())
                logger.debug("Command to execute: " + commandLine);

            List<String> commandArgs = commandLineParser.parse(commandLine);
            UnixCommand command = new UnixCommand(hostName, commandArgs);
            Callable<?> callable = getCallable(command);
            Future<?> future = threadPool.submit(callable);
            futures.add(future);
        }

        // Build up a list of all the results and/or capture the errors as they
        // occur.
        try {
            StringBuilder errors = new StringBuilder();

            for(Future<?> future: futures) {
                Throwable t = null;

                try {
                    future.get();
                } catch(ExecutionException ex) {
                    t = ex.getCause();
                } catch(Exception e) {
                    t = e;
                }

                if(t != null) {
                    if(logger.isWarnEnabled())
                        logger.warn(t, t);

                    if(errors.length() > 0)
                        errors.append("; ");

                    errors.append(t.getMessage());
                }
            }

            if(errors.length() > 0)
                throw new RemoteOperationException(errors.toString());
        } finally {
            threadPool.shutdown();

            try {
                threadPool.awaitTermination(60, TimeUnit.SECONDS);
            } catch(InterruptedException e) {
                if(logger.isWarnEnabled())
                    logger.warn(e, e);
            }
        }
View Full Code Here

TOP

Related Classes of java.util.concurrent.ExecutorService

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.