Example usage for java.util.concurrent ExecutorService shutdownNow

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

Introduction

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

Prototype

List<Runnable> shutdownNow();

Source Link

Document

Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.

Usage

From source file:io.fabric8.kubernetes.pipeline.BuildImageStepExecution.java

@Override
protected ImageInspect run() throws Exception {
    return workspace.getChannel().call(new MasterToSlaveCallable<ImageInspect, Exception>() {
        @Override// w w  w . j av a  2s. c o m
        public ImageInspect call() throws Exception {
            ExecutorService executorService = Executors.newFixedThreadPool(2);
            try {
                Future<Boolean> createTarFuture;
                Future<ImageInspect> buildImageFuture;
                try (PipedInputStream pin = new PipedInputStream();
                        PipedOutputStream pout = new PipedOutputStream(pin)) {

                    createTarFuture = executorService.submit(new CreateTarTask(pout));
                    buildImageFuture = executorService.submit(new BuildImageTask(pin));
                }

                //Wait for the two tasks to complete.
                if (!createTarFuture.get(step.getTimeout(), TimeUnit.MILLISECONDS)) {
                    listener.getLogger().println("Failed to create docker image tarball.");
                }

                ImageInspect imageInspect = buildImageFuture.get(step.getTimeout(), TimeUnit.MILLISECONDS);
                if (imageInspect == null) {
                    throw new RuntimeException("Failed to build docker image.");
                } else {
                    return imageInspect;
                }
            } finally {
                executorService.shutdown();
                if (executorService.awaitTermination(30, TimeUnit.SECONDS)) {
                    executorService.shutdownNow();
                }
            }
        }
    });
}

From source file:fsm.series.Series_CF.java

/**
 * Computes the 5 integrals simultaneously for increased performance.
 *
 * @param m Fourier term row/*  w w  w . j  a  v a2  s.  c  o m*/
 * @param n Fourier term column
 * @return double array of size 5 with indexes corresponding to integral
 * number (1-5)
 */
@Override
public double[] getIntegralValues(int m, int n) {

    double[] I = new double[5];

    Callable<Double> tsk1 = () -> getI1(m, n);

    Callable<Double> tsk2 = () -> getI2(m, n);

    Callable<Double> tsk3 = () -> getI3(m, n);

    Callable<Double> tsk4 = () -> getI4(m, n);

    Callable<Double> tsk5 = () -> getI5(m, n);

    ExecutorService service;
    final Future<Double> thread1, thread2, thread3, thread4, thread5;

    service = Executors.newFixedThreadPool(5);
    thread1 = service.submit(tsk1);
    thread2 = service.submit(tsk2);
    thread3 = service.submit(tsk3);
    thread4 = service.submit(tsk4);
    thread5 = service.submit(tsk5);

    try {
        I[0] = thread1.get();
    } catch (InterruptedException | ExecutionException ex) {
        Logger.getLogger(Series_CF.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        I[1] = thread2.get();
    } catch (InterruptedException | ExecutionException ex) {
        Logger.getLogger(Series_CF.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        I[2] = thread3.get();
    } catch (InterruptedException | ExecutionException ex) {
        Logger.getLogger(Series_CF.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        I[3] = thread4.get();
    } catch (InterruptedException | ExecutionException ex) {
        Logger.getLogger(Series_CF.class.getName()).log(Level.SEVERE, null, ex);
    }

    try {
        I[4] = thread5.get();
    } catch (InterruptedException | ExecutionException ex) {
        Logger.getLogger(Series_CF.class.getName()).log(Level.SEVERE, null, ex);
    }

    service.shutdownNow();

    return I;
}

From source file:org.m2x.rssreader.service.FetcherService.java

private int refreshFeeds() {
    ContentResolver cr = getContentResolver();
    final Cursor cursor = cr.query(FeedColumns.CONTENT_URI, FeedColumns.PROJECTION_ID, null, null, null);
    int nbFeed = cursor.getCount();

    ExecutorService executor = Executors.newFixedThreadPool(THREAD_NUMBER, new ThreadFactory() {
        @Override/*w  w w  .  jav  a  2  s  .c  om*/
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setPriority(Thread.MIN_PRIORITY);
            return t;
        }
    });

    CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(executor);
    while (cursor.moveToNext()) {
        final String feedId = cursor.getString(0);
        completionService.submit(new Callable<Integer>() {
            @Override
            public Integer call() {
                int result = 0;
                try {
                    result = refreshFeed(feedId);
                } catch (Exception ignored) {
                }
                return result;
            }
        });
    }
    cursor.close();

    int globalResult = 0;
    for (int i = 0; i < nbFeed; i++) {
        try {
            Future<Integer> f = completionService.take();
            globalResult += f.get();
        } catch (Exception ignored) {
        }
    }

    executor.shutdownNow(); // To purge all threads

    return globalResult;
}

From source file:com.serphacker.serposcope.task.proxy.ProxyChecker.java

@Override
public void run() {

    LOG.info("starting proxy checking task, threads = {}, timeout in MS = {}", nThread, timeoutMS);

    long start = System.currentTimeMillis();

    List<Proxy> proxies = db.proxy.list();
    if (proxies == null || proxies.isEmpty()) {
        LOG.debug("no proxy to check");
        return;/*  w w w .j ava2 s  .  c  o  m*/
    }

    totalProxies = proxies.size();

    ExecutorService executor = Executors.newFixedThreadPool(nThread);
    db.proxy.updateStatus(Proxy.Status.UNCHECKED,
            proxies.stream().map((t) -> t.getId()).collect(Collectors.toList()));

    for (Proxy proxy : proxies) {
        executor.submit(new Runnable() {
            @Override
            public void run() {
                ScrapClient cli = new ScrapClient();

                cli.setTimeout(timeoutMS);
                ScrapProxy scrapProxy = proxy.toScrapProxy();
                cli.setProxy(scrapProxy);

                LOG.info("checking {}", scrapProxy);

                Proxy.Status proxyStatus = Proxy.Status.ERROR;

                //                    try{Thread.sleep(30000l);}catch(Exception ex){}

                int httpStatus = cli.get(judgeUrl);
                if (httpStatus == 200 && cli.getContentAsString() != null) {
                    Matcher matcher = PATTERN_IP.matcher(cli.getContentAsString());
                    if (matcher.find()) {
                        proxy.setRemoteip(matcher.group(1));
                        proxyStatus = Proxy.Status.OK;
                    }
                }

                proxy.setStatus(proxyStatus);
                proxy.setLastCheck(LocalDateTime.now());
                db.proxy.update(proxy);

                checked.incrementAndGet();
            }
        });
    }

    executor.shutdown();
    try {
        executor.awaitTermination(1, TimeUnit.HOURS);
    } catch (InterruptedException ex) {
        executor.shutdownNow();
    }
    LOG.info("proxy checking finished in {}",
            DurationFormatUtils.formatDurationHMS(System.currentTimeMillis() - start));
}

From source file:org.structr.common.SystemTest.java

@Test
public void testTransactionIsolationWithFailures() {

    Settings.CypherDebugLogging.setValue(true);

    // Tests the transaction isolation of the underlying database layer.

    // Create a node and use ten different threads to set a property on
    // it in a transaction. Observe the property value to check that the
    // threads do not interfere with each other.

    try {/*from w w  w  . j  av  a  2  s. c  o m*/

        final TestOne test = createTestNode(TestOne.class);
        final ExecutorService executor = Executors.newCachedThreadPool();
        final List<FailingTestRunner> tests = new LinkedList<>();
        final List<Future> futures = new LinkedList<>();

        // create and run test runners
        for (int i = 0; i < 25; i++) {

            final FailingTestRunner runner = new FailingTestRunner(app, test);

            futures.add(executor.submit(runner));
            tests.add(runner);
        }

        // wait for termination
        for (final Future future : futures) {
            future.get();
            System.out.print(".");
        }

        System.out.println();

        // check for success
        for (final FailingTestRunner runner : tests) {
            assertTrue("Could not validate transaction isolation", runner.success());
        }

        executor.shutdownNow();

    } catch (Throwable fex) {
        fex.printStackTrace();
        fail("Unexpected exception");
    }

    Settings.CypherDebugLogging.setValue(false);
}

From source file:org.structr.common.SystemTest.java

@Test
public void testConcurrentIdenticalRelationshipCreation() {

    try {//from   w w w.  j av  a2  s .  c o  m
        final ExecutorService service = Executors.newCachedThreadPool();
        final TestSix source = createTestNode(TestSix.class);
        final TestOne target = createTestNode(TestOne.class);

        final Future one = service.submit(new RelationshipCreator(source, target));
        final Future two = service.submit(new RelationshipCreator(source, target));

        // wait for completion
        one.get();
        two.get();

        try (final Tx tx = app.tx()) {

            // check for a single relationship since all three parts of
            // both relationships are equal => only one should be created
            final List<TestOne> list = source.getProperty(TestSix.oneToManyTestOnes);

            assertEquals("Invalid concurrent identical relationship creation result", 1, list.size());

            tx.success();
        }

        service.shutdownNow();

    } catch (ExecutionException | InterruptedException | FrameworkException fex) {
        logger.warn("", fex);
    }
}

From source file:org.apache.hadoop.mapred.TestJvmManager.java

/**
 * Create a bunch of tasks and use a special hash map to detect
 * racy access to the various internal data structures of JvmManager.
 * (Regression test for MAPREDUCE-2224)//  ww w  .  ja v  a  2 s . co m
 */
@Test
public void testForRaces() throws Exception {
    JvmManagerForType mapJvmManager = jvmManager.getJvmManagerForType(TaskType.MAP);

    // Sub out the HashMaps for maps that will detect racy access.
    mapJvmManager.jvmToRunningTask = new RaceHashMap<JVMId, TaskRunner>();
    mapJvmManager.runningTaskToJvm = new RaceHashMap<TaskRunner, JVMId>();
    mapJvmManager.jvmIdToRunner = new RaceHashMap<JVMId, JvmRunner>();

    // Launch a bunch of JVMs, but only allow MAP_SLOTS to run at once.
    final ExecutorService exec = Executors.newFixedThreadPool(MAP_SLOTS);
    final AtomicReference<Throwable> failed = new AtomicReference<Throwable>();

    for (int i = 0; i < MAP_SLOTS * 5; i++) {
        JobConf taskConf = new JobConf(ttConf);
        TaskAttemptID attemptID = new TaskAttemptID("test", 0, TaskType.MAP, i, 0);
        Task task = new MapTask(null, attemptID, i, null, 1);
        task.setConf(taskConf);
        TaskInProgress tip = tt.new TaskInProgress(task, taskConf);
        File pidFile = new File(TEST_DIR, "pid_" + i);
        final TaskRunner taskRunner = task.createRunner(tt, tip);
        // launch a jvm which sleeps for 60 seconds
        final Vector<String> vargs = new Vector<String>(2);
        vargs.add(writeScript("script_" + i, "echo hi\n", pidFile).getAbsolutePath());
        final File workDir = new File(TEST_DIR, "work_" + i);
        workDir.mkdir();
        final File stdout = new File(TEST_DIR, "stdout_" + i);
        final File stderr = new File(TEST_DIR, "stderr_" + i);

        // launch the process and wait in a thread, till it finishes
        Runnable launcher = new Runnable() {
            public void run() {
                try {
                    taskRunner.launchJvmAndWait(null, vargs, stdout, stderr, 100, workDir, null);
                } catch (Throwable t) {
                    failed.compareAndSet(null, t);
                    exec.shutdownNow();
                    return;
                }
            }
        };
        exec.submit(launcher);
    }

    exec.shutdown();
    exec.awaitTermination(3, TimeUnit.MINUTES);
    if (failed.get() != null) {
        throw new RuntimeException(failed.get());
    }
}

From source file:com.tasktop.c2c.server.ssh.server.commands.AbstractInteractiveProxyCommand.java

protected void performCommand(Environment env, ProjectService service, String projectId, String path,
        String requestPath, RequestHeadersSupport headers) throws CommandException {
    String internalProxyUri = service.computeInternalProxyBaseUri(false);
    if (internalProxyUri == null) {
        throw new IllegalStateException();
    }//from   w  w  w. ja  va 2s.  c  o  m
    URI targetUri;
    try {
        if (!internalProxyUri.endsWith("/")) {
            internalProxyUri += "/";
        }
        internalProxyUri += getName() + '/' + path;

        targetUri = new URI(internalProxyUri);
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
    String host = targetUri.getHost();
    int port = targetUri.getPort();
    if (port < 0) {
        port = 80;
    }
    if (targetUri.getScheme() == null || !targetUri.getScheme().equalsIgnoreCase("http")) {
        throw new IllegalStateException("scheme " + targetUri.getScheme() + " is not supported");
    }
    HeaderGroup headerGroup = computeHeaders(targetUri);
    for (Entry<String, List<String>> headerEntry : headers.getRequestHeaders().entrySet()) {
        for (String value : headerEntry.getValue()) {
            headerGroup.addHeader(new Header(headerEntry.getKey(), value));
        }
    }
    getLogger().info("Proxying " + getName() + " to " + targetUri);
    try {
        Socket socket = socketFactory.openConnection(host, port);
        try {
            // initiate an HTTP request with Transfer-Encoding: chunked
            OutputStream proxyOut = socket.getOutputStream();
            emitHttpRequestLine(proxyOut, targetUri);
            emitHeaders(proxyOut, headerGroup);

            proxyOut.flush();

            List<Callable<Void>> tasks = new ArrayList<Callable<Void>>(3);
            FlushingChunkedOutputStream chunkedRequestOut = new FlushingChunkedOutputStream(proxyOut);
            tasks.add(new InputPipe(in, chunkedRequestOut, bufferSize, Thread.currentThread()).flush(true));

            // start these pipes
            ExecutorService executor = Executors.newFixedThreadPool(tasks.size());
            try {
                for (Callable<Void> task : tasks) {
                    executor.submit(task);
                }

                InputStream proxyInput = socket.getInputStream();
                try {
                    readHttpResponse(proxyInput);
                    MultiplexingInputStream input = new MultiplexingInputStream(
                            new ChunkedInputStream(proxyInput));
                    for (;;) {
                        PacketType packetType = input.getPacketType();
                        if (packetType == null) {
                            break;
                        }
                        int length = input.getPacketLength();

                        processData(input, packetType, length);
                    }
                } finally {
                    try {
                        executor.shutdown();
                        executor.awaitTermination(1000L, TimeUnit.MILLISECONDS);
                    } catch (InterruptedException e) {
                        // ignore
                    }
                }
            } finally {
                executor.shutdownNow();
                try {
                    executor.awaitTermination(3000L, TimeUnit.MILLISECONDS);
                } catch (InterruptedException e) {
                    // ignore
                }
                Thread.interrupted();

                try {
                    // attempt to close the chunked output, since this will make us a well-behaved client
                    // by sending the closing chunk.
                    chunkedRequestOut.close();
                } catch (Throwable t) {
                    // ignore
                }
            }
        } finally {
            socket.close();
        }
    } catch (ConnectException e) {
        getLogger().error(e.getMessage(), e);
        throw new CommandException(-1, "Service temporarily unavailable");
    } catch (IOException e) {
        getLogger().warn(e.getMessage(), e);
        throw new CommandException(-1, e.getMessage());
    }
}

From source file:org.apache.bookkeeper.tools.perf.dlog.PerfWriter.java

void execute(Namespace namespace) throws Exception {
    List<Pair<Integer, DistributedLogManager>> managers = new ArrayList<>(flags.numLogs);
    for (int i = 0; i < flags.numLogs; i++) {
        String logName = String.format(flags.logName, i);
        managers.add(Pair.of(i, namespace.openLog(logName)));
    }//from w w w  .j  a v  a 2s.co  m
    log.info("Successfully open {} logs", managers.size());

    // register shutdown hook to aggregate stats
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        isDone.set(true);
        printAggregatedStats(cumulativeRecorder);
    }));

    ExecutorService executor = Executors.newFixedThreadPool(flags.numThreads);
    try {
        for (int i = 0; i < flags.numThreads; i++) {
            final int idx = i;
            final List<DistributedLogManager> logsThisThread = managers.stream()
                    .filter(pair -> pair.getLeft() % flags.numThreads == idx).map(pair -> pair.getRight())
                    .collect(Collectors.toList());
            final long numRecordsForThisThread = flags.numRecords / flags.numThreads;
            final long numBytesForThisThread = flags.numBytes / flags.numThreads;
            final double writeRateForThisThread = flags.writeRate / (double) flags.numThreads;
            final long maxOutstandingBytesForThisThread = flags.maxOutstandingMB * 1024 * 1024
                    / flags.numThreads;
            executor.submit(() -> {
                try {
                    write(logsThisThread, writeRateForThisThread, (int) maxOutstandingBytesForThisThread,
                            numRecordsForThisThread, numBytesForThisThread);
                } catch (Exception e) {
                    log.error("Encountered error at writing records", e);
                }
            });
        }
        log.info("Started {} write threads", flags.numThreads);
        reportStats();
    } finally {
        executor.shutdown();
        if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
            executor.shutdownNow();
        }
        managers.forEach(manager -> manager.getRight().asyncClose());
    }
}

From source file:com.github.kubernetes.java.client.live.KubernetesApiClientLiveTest.java

@Test
public void testCreateReplicationController() throws Exception {
    if (log.isDebugEnabled()) {
        log.debug("Creating a Replication Controller: " + contr);
    }/*from ww  w  . j av  a 2s  .c  om*/
    getClient().createReplicationController(contr);
    assertNotNull(getClient().getReplicationController(contr.getId()));

    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<PodList> future = executor.submit(new Callable<PodList>() {
        public PodList call() throws Exception {
            PodList pods;
            do {
                log.info("Waiting for Pods to be ready");
                Thread.sleep(1000);
                pods = getClient()
                        .getSelectedPods(ImmutableMap.of("name", "kubernetes-test-controller-selector"));
                if (pods.isEmpty()) {
                    continue;
                }

                StateInfo info = pods.get(0).getCurrentState().getInfo("kubernetes-test");
                if ((info != null) && info.getState("waiting") != null) {
                    throw new RuntimeException("Pod is waiting due to " + info.getState("waiting"));
                }
            } while (pods.isEmpty() || !FluentIterable.from(pods).allMatch(new Predicate<Pod>() {
                public boolean apply(Pod pod) {
                    return "Running".equals(pod.getCurrentState().getStatus());
                }
            }));
            return pods;
        }
    });

    PodList pods;
    try {
        pods = future.get(90, TimeUnit.SECONDS);
    } finally {
        executor.shutdownNow();
    }
    for (Pod pod : pods) {
        assertNotNull(pod.getCurrentState().getInfo("kubernetes-test").getState("running"));
        assertNotNull(pod.getCurrentState().getNetInfo().getState("running"));
    }

    // test recreation using same id
    try {
        getClient().createReplicationController(contr);
        fail("Should have thrown exception");
    } catch (Exception e) {
        // ignore
    }
    assertNotNull(getClient().getReplicationController(contr.getId()));

    PodList podList = getClient().getSelectedPods(contr.getLabels());
    assertNotNull(podList);
    assertNotNull(podList.getItems());
    assertEquals(contr.getDesiredState().getReplicas(), podList.getItems().size());
}