Example usage for java.util.concurrent FutureTask cancel

List of usage examples for java.util.concurrent FutureTask cancel

Introduction

In this page you can find the example usage for java.util.concurrent FutureTask cancel.

Prototype

public boolean cancel(boolean mayInterruptIfRunning) 

Source Link

Usage

From source file:com.taobao.tair.comm.TairClientFactory.java

public void close() {
    for (FutureTask<TairClient> task : clients.values()) {
        if (task.isDone() || !task.cancel(true)) {
            TairClient client = null;/*from  www . j  a v  a2 s.c o  m*/
            try {
                client = task.get();
            } catch (InterruptedException e) {
                LOGGER.warn(e);
            } catch (ExecutionException e) {
                LOGGER.warn(e);
            } catch (CancellationException e) {
            }
            client.close();
        }
    }
    clients.clear();
}

From source file:de.uni_rostock.goodod.owl.OntologyCache.java

public synchronized void flushCache() {
    for (Entry<URI, FutureTask<OWLOntology>> f : futures.entrySet()) {
        FutureTask<OWLOntology> future = f.getValue();
        if (false == future.isDone()) {
            future.cancel(true);
        }//www  .  ja  v  a 2s .c o m
    }
    futures.clear();
}

From source file:org.openconcerto.sql.view.list.UpdateQueue.java

protected void willPut(final Runnable qr) throws InterruptedException {
    if (qr instanceof ChangeAllRunnable) {
        // si on met tout  jour, ne sert  rien de garder les maj prcdentes.
        // ATTN aux runnables qui dpendent des update, si on enlve les maj
        // elles vont s'executer sans que sa maj soit faite
        this.tasksDo(new IClosure<Deque<FutureTask<?>>>() {
            @Override/*from   w  w w.  j  av a  2 s  .co m*/
            public void executeChecked(final Deque<FutureTask<?>> tasks) {
                // on part de la fin et on supprime toutes les maj jusqu'a ce qu'on trouve
                // un runnable qui n'est pas un UpdateRunnable
                FutureTask<?> current = tasks.peekLast();
                boolean onlyUpdateRunnable = true;
                while (current != null && onlyUpdateRunnable) {
                    onlyUpdateRunnable = isCancelableUpdate(current);
                    if (onlyUpdateRunnable) {
                        tasks.removeLast();
                        current = tasks.peekLast();
                    }
                }
                if (onlyUpdateRunnable) {
                    final FutureTask<?> br = getBeingRun();
                    if (br != null && isCancelableUpdate(br))
                        br.cancel(true);
                }
            }
        });
    }
}

From source file:de.uni_rostock.goodod.owl.OntologyCache.java

public synchronized void removeOntologyAtURI(URI u) {
    FutureTask<OWLOntology> future = futures.get(u);
    if (null == future) {
        return;// w ww.j  a v a  2s.co  m
    } else if (false == future.isDone()) {
        future.cancel(true);
    }
    futures.remove(u);
}

From source file:gridool.construct.GridTaskAdapter.java

public boolean cancel() throws GridException {
    final FutureTask<Serializable> r = running;
    if (r == null) {
        return false;
    }/*from  w w  w .  j a v a 2  s. c o m*/
    boolean status = r.cancel(true);
    this.canceled = status;
    return status;
}

From source file:test.com.azaptree.services.executor.ThreadPoolExecutorTest.java

@Test
public void testPurge() {
    pauseableExecutor.setCorePoolSize(1);
    pauseableExecutor.setMaximumPoolSize(1);
    pauseableExecutor.pause();/* ww  w.  j a  va 2 s  .  co m*/
    for (int i = 0; i < 100; i++) {
        final FutureTask<Boolean> futureTask = new FutureTask<>(new Task(), Boolean.TRUE);
        pauseableExecutor.execute(futureTask);
        futureTask.cancel(true);
    }
    Assert.assertEquals(pauseableExecutor.getQueue().size(), 99);
    pauseableExecutor.purge();
    Assert.assertEquals(pauseableExecutor.getQueue().size(), 0);
    pauseableExecutor.resume();
}

From source file:org.gytheio.health.CompositeComponentUnavailableAction.java

protected void execute(final ComponentUnavailableAction action, final Throwable e) throws Throwable {
    FutureTask<Void> task = null;
    try {//ww w  .j av a2  s .  c o m
        task = new FutureTask<Void>(new Runnable() {
            @Override
            public void run() {
                action.execute(e);
            }
        }, null);

        getExecutorService().execute(task);
        task.get(actionTimeoutMs, TimeUnit.MILLISECONDS);
    } catch (TimeoutException e1) {
        task.cancel(true);
        throw e;
    } catch (InterruptedException e1) {
        // We were asked to stop
        task.cancel(true);
    } catch (ExecutionException e1) {
        // Unwrap our cause and throw that
        Throwable cause = e.getCause();
        throw cause;
    }
}

From source file:org.apache.nutch.parse.ParseUtil.java

private ParseResult runParser(Parser p, Content content) {
    ParseCallable pc = new ParseCallable(p, content);
    FutureTask<ParseResult> task = new FutureTask<ParseResult>(pc);
    ParseResult res = null;//from  w  w w .j ava 2s . c  om
    Thread t = new Thread(task);
    t.start();
    try {
        res = task.get(MAX_PARSE_TIME, TimeUnit.SECONDS);
    } catch (TimeoutException e) {
        LOG.warn("TIMEOUT parsing " + content.getUrl() + " with " + p);
    } catch (Exception e) {
        task.cancel(true);
        res = null;
        t.interrupt();
    } finally {
        t = null;
        pc = null;
    }
    return res;
}

From source file:cognition.common.service.DocumentConversionService.java

private File makeTiffFromPDF(DNCWorkCoordinate coordinate, File input) throws IOException, TikaException {
    File output = File.createTempFile(coordinate.getFileName(), ".tiff");
    String[] cmd = { getImageMagickProg(), "-density", "300", input.getPath(), "-depth", "8", "-quality", "1",
            output.getPath() };//ww w.j  ava 2s . c  o  m
    Process process = new ProcessBuilder(cmd).start();
    IOUtils.closeQuietly(process.getOutputStream());
    InputStream processInputStream = process.getInputStream();
    logStream(processInputStream);
    FutureTask<Integer> waitTask = new FutureTask<>(process::waitFor);
    Thread waitThread = new Thread(waitTask);
    waitThread.start();
    try {
        waitTask.get(240, TimeUnit.SECONDS);
        return output;
    } catch (Exception e) {
        logger.error(e.getMessage());
        waitThread.interrupt();
        process.destroy();
        waitTask.cancel(true);
    } finally {
        IOUtils.closeQuietly(processInputStream);
        process.destroy();
        waitThread.interrupt();
        waitTask.cancel(true);
    }
    return null;
}

From source file:org.apache.qpid.server.security.access.firewall.HostnameFirewallRule.java

/**
 * @param remote//  w  w  w .ja  v  a 2  s  .co  m
 *            the InetAddress to look up
 * @return the hostname, null if not found, takes longer than
 *         {@value #DNS_LOOKUP} to find or otherwise fails
 */
private String getHostname(final InetAddress remote) throws AccessControlFirewallException {
    FutureTask<String> lookup = new FutureTask<String>(new Callable<String>() {
        public String call() {
            return remote.getCanonicalHostName();
        }
    });
    DNS_LOOKUP.execute(lookup);

    try {
        return lookup.get(DNS_TIMEOUT, TimeUnit.MILLISECONDS);
    } catch (Exception e) {
        _logger.warn("Unable to look up hostname from address " + remote, e);
        return null;
    } finally {
        lookup.cancel(true);
    }
}