Example usage for java.lang Thread interrupt

List of usage examples for java.lang Thread interrupt

Introduction

In this page you can find the example usage for java.lang Thread interrupt.

Prototype

public void interrupt() 

Source Link

Document

Interrupts this thread.

Usage

From source file:net.bpelunit.framework.control.run.TestCaseRunner.java

private void interruptAllThreads(final List<Thread> threads) {
    fLogger.debug("Trying to interrupt all threads...");
    for (Thread t : threads) {
        if (t.isAlive()) {
            t.interrupt();
        }//from   w ww .  j av  a2s  .c  om
    }
    fLogger.debug("All threads interrupted. Waiting for threads...");
}

From source file:com.espertech.esper.dataflow.core.EPDataFlowInstanceImpl.java

public void cancel() {
    if (state == EPDataFlowState.COMPLETE || state == EPDataFlowState.CANCELLED) {
        return;/*w w w.j a v  a 2 s.c om*/
    }
    if (state == EPDataFlowState.INSTANTIATED) {
        setState(EPDataFlowState.CANCELLED);
        sourceRunnables.clear();
        callOperatorClose();
        return;
    }

    // handle async start
    if (threads != null) {
        for (GraphSourceRunnable runnable : sourceRunnables) {
            runnable.shutdown();
        }
        for (Thread thread : threads) {
            if (thread.isAlive() && !thread.isInterrupted()) {
                thread.interrupt();
            }
        }
    }
    // handle run
    else {
        if (runCurrentThread != null) {
            runCurrentThread.interrupt();
        }
        runCurrentThread = null;
    }

    callOperatorClose();

    setState(EPDataFlowState.CANCELLED);
    sourceRunnables.clear();
}

From source file:rnaedit.test.IOUtil.java

public static final void readWrite(final InputStream remoteInput,

        final OutputStream remoteOutput,

        final InputStream localInput,

        final OutputStream localOutput)

{

    Thread reader, writer;

    reader = new Thread()

    {//from   w  w  w. j  a  v a  2  s  .com

        public void run()

        {

            int ch;

            try

            {

                while (!interrupted() && (ch = localInput.read()) != -1)

                {

                    remoteOutput.write(ch);

                    remoteOutput.flush();

                }

            }

            catch (IOException e)

            {

                //e.printStackTrace();

            }

        }

    }

    ;

    writer = new Thread()

    {

        public void run()

        {

            try

            {

                Util.copyStream(remoteInput, localOutput);

            }

            catch (IOException e)

            {

                e.printStackTrace();

                System.exit(1);

            }

        }

    };

    writer.setPriority(Thread.currentThread().getPriority() + 1);

    writer.start();

    reader.setDaemon(true);

    reader.start();

    try

    {

        writer.join();

        reader.interrupt();

    }

    catch (InterruptedException e)

    {

    }

}

From source file:com.strumsoft.websocket.phonegap.WebSocketFactory.java

public WebSocket getInstance(String url, WebSocket.Draft draft) {
    WebSocket socket = null;/*from w  w  w  . j  a  va2s .c  om*/
    Thread th = null;
    try {
        socket = new WebSocket(appView, new URI(url), draft, getRandonUniqueId());
        th = socket.connect();
        return socket;
    } catch (Exception e) {
        //Log.v("websocket", e.toString());
        if (th != null) {
            th.interrupt();
        }
    }
    return null;
}

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() };/*from ww w.  j a  v  a  2s. co  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:gobblin.runtime.app.ServiceBasedAppLauncher.java

private void addInterruptedShutdownHook() {
    final Thread mainThread = Thread.currentThread();

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override/*from   w  w w . ja v  a  2s  . co  m*/
        public void run() {
            mainThread.interrupt();
        }
    });
}

From source file:uk.ac.kcl.iop.brc.core.pipeline.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() };/*from w w w.  j ava2 s .c om*/
    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();
        Thread.currentThread().interrupt();
        waitTask.cancel(true);
    } finally {
        IOUtils.closeQuietly(processInputStream);
        process.destroy();
        waitThread.interrupt();
        waitTask.cancel(true);
    }
    return null;
}

From source file:com.nesscomputing.event.jms.JmsEventReceiver.java

@OnStage(LifecycleStage.STOP)
public void stop() {
    final Thread consumerThread = consumerThreadHolder.getAndSet(null);
    if (consumerThread != null) {
        try {/*from w  ww.  j a v  a  2s . c om*/
            final AbstractConsumer consumer = consumerHolder.getAndSet(null);
            if (consumer != null) {
                consumer.shutdown();

                consumerThread.interrupt();
                consumerThread.join(500L);
            }
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt(); // Someone else needs to handle that.
        }
    } else {
        LOG.debug("Never started, ignoring stop()");
    }
}

From source file:org.apache.hadoop.raid.DirectoryTraversal.java

private void interruptProcessors() {
    for (Thread processor : processors) {
        if (processor != null) {
            processor.interrupt();
        }// ww  w  . ja  v a 2s . c o m
    }
}

From source file:com.streamsets.datacollector.main.TestLogConfigurator.java

@After
public void cleanUp() throws Exception {
    System.getProperties().remove("log4j.configuration");
    System.getProperties().remove("log4j.defaultInitOverride");
    for (Thread thread : Thread.getAllStackTraces().keySet()) {
        if (thread instanceof FileWatchdog) {
            Field interrupted = ((Class) thread.getClass().getGenericSuperclass())
                    .getDeclaredField("interrupted");
            interrupted.setAccessible(true);
            interrupted.set(thread, true);
            thread.interrupt();
        }//from  w  w w. j  a v a 2  s.c  om
    }
}