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:org.ulyssis.ipp.publisher.HttpServerPublisher.java

@Override
public void run() {
    LOG.info("Starting Undertow!");
    Thread thread = new Thread(server::start);
    thread.start();//w ww .j  a  v  a 2s  .c  om
    try {
        while (!Thread.currentThread().isInterrupted()) {
            Thread.sleep(10L);
        }
    } catch (InterruptedException ignored) {
    }
    server.stop();
    thread.interrupt();
    try {
        thread.join();
    } catch (InterruptedException ignored) {
    }
    cleanup();
}

From source file:org.apache.hadoop.corona.MiniCoronaCluster.java

public void shutdown() {
    try {//from   ww w . j av a 2 s  .  c  o m
        waitTaskTrackers();
        for (int idx = 0; idx < taskTrackerList.size(); idx++) {
            TaskTrackerRunner taskTracker = taskTrackerList.get(idx);
            Thread taskTrackerThread = taskTrackerThreadList.get(idx);
            taskTracker.shutdown();
            taskTrackerThread.interrupt();
            try {
                taskTrackerThread.join();
            } catch (InterruptedException ex) {
                LOG.error("Problem shutting down task tracker", ex);
            }
        }
    } finally {
        File configDir = new File("build", "minimr");
        File siteFile = new File(configDir, "mapred-site.xml");
        siteFile.delete();
    }
    try {
        pjt.shutdown();
        pjt.join();
    } catch (Exception e) {
        LOG.error("Error during PJT shutdown", e);
    }
    try {
        clusterManagerServer.stopRunning();
        clusterManagerServer.join();
    } catch (Exception e) {
        LOG.error("Error during CM shutdown", e);
    }
}

From source file:org.apache.hadoop.mapreduce.v2.app.commit.CommitterEventHandler.java

private synchronized void cancelJobCommit() {
    Thread threadCommitting = jobCommitThread;
    if (threadCommitting != null && threadCommitting.isAlive()) {
        LOG.info("Cancelling commit");
        threadCommitting.interrupt();

        // wait up to configured timeout for commit thread to finish
        long now = context.getClock().getTime();
        long timeoutTimestamp = now + commitThreadCancelTimeoutMs;
        try {//from   ww w .  j a  v a 2 s. com
            while (jobCommitThread == threadCommitting && now > timeoutTimestamp) {
                wait(now - timeoutTimestamp);
                now = context.getClock().getTime();
            }
        } catch (InterruptedException e) {
        }
    }
}

From source file:net.xmind.workbench.internal.notification.SiteEventNotificationService.java

private void stop() {
    if (job != null) {
        Thread thread = job.getThread();
        job.cancel();/*  www . j a v  a 2 s.  co  m*/
        if (thread != null) {
            thread.interrupt();
        }
        job = null;
    }
}

From source file:grails.plugins.jesque.AdminImpl.java

/**
 * {@inheritDoc}/*from  w  w  w  .  jav  a  2s .  com*/
 */
@Override
public void end(final boolean now) {
    this.state.set(SHUTDOWN);
    this.jedisPubSub.unsubscribe();
    if (now) {
        final Thread workerThread = this.threadRef.get();
        if (workerThread != null) {
            workerThread.interrupt();
        }
    }
}

From source file:org.spoutcraft.launcher.api.util.Download.java

@SuppressWarnings("unused")
public void run() {
    ReadableByteChannel rbc = null;
    FileOutputStream fos = null;/*  w  w  w. j av  a2  s  .c o  m*/
    try {
        URLConnection conn = url.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(false);
        System.setProperty("http.agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19");
        conn.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19");
        HttpURLConnection.setFollowRedirects(true);
        conn.setUseCaches(false);
        ((HttpURLConnection) conn).setInstanceFollowRedirects(true);
        int response = ((HttpURLConnection) conn).getResponseCode();
        InputStream in = getConnectionInputStream(conn);

        size = conn.getContentLength();
        outFile = new File(outPath);
        outFile.delete();

        rbc = Channels.newChannel(in);
        fos = new FileOutputStream(outFile);

        stateChanged();

        Thread progress = new MonitorThread(Thread.currentThread(), rbc);
        progress.start();

        fos.getChannel().transferFrom(rbc, 0, size > 0 ? size : Integer.MAX_VALUE);
        in.close();
        rbc.close();
        progress.interrupt();
        if (size > 0) {
            if (size == outFile.length()) {
                result = Result.SUCCESS;
            }
        } else {
            result = Result.SUCCESS;
        }
    } catch (PermissionDeniedException e) {
        result = Result.PERMISSION_DENIED;
    } catch (DownloadException e) {
        result = Result.FAILURE;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(rbc);
    }
}

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 ww  w.ja  va 2 s.  c o  m
    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:org.echocat.jemoni.carbon.jmx.Jmx2CarbonBridge.java

protected void stopThread(@Nullable Thread thread) {
    if (thread != null) {
        thread.interrupt();
        try {//from   w ww .  j a va2  s.  co  m
            long tries = 0;
            while (thread.isAlive()) {
                tries++;
                thread.join(50);
                thread.interrupt();
                if (thread.isAlive() && tries % 200 == 0) {
                    LOG.info("Still wait for termination of '" + thread + "'...");
                }
            }
        } catch (InterruptedException ignored) {
            currentThread().interrupt();
            LOG.debug("Could not wait for termination of '" + thread + "' - but this thread was interrupted.");
        }
    }
}

From source file:org.spoutcraft.launcher.util.Download.java

@SuppressWarnings("unused")
public void run() {
    ReadableByteChannel rbc = null;
    FileOutputStream fos = null;//from   ww  w.  j  av  a2 s  .  c  om
    try {
        URLConnection conn = url.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(false);
        System.setProperty("http.agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19");
        conn.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19");
        HttpURLConnection.setFollowRedirects(true);
        conn.setUseCaches(false);
        ((HttpURLConnection) conn).setInstanceFollowRedirects(true);
        int response = ((HttpURLConnection) conn).getResponseCode();
        InputStream in = getConnectionInputStream(conn);

        size = conn.getContentLength();
        outFile = new File(outPath);
        outFile.delete();

        rbc = Channels.newChannel(in);
        fos = new FileOutputStream(outFile);

        stateChanged();

        Thread progress = new MonitorThread(Thread.currentThread(), rbc);
        progress.start();

        fos.getChannel().transferFrom(rbc, 0, size > 0 ? size : Integer.MAX_VALUE);
        in.close();
        rbc.close();
        progress.interrupt();
        if (size > 0) {
            if (size == outFile.length()) {
                result = Result.SUCCESS;
            }
        } else {
            result = Result.SUCCESS;
        }
    } catch (PermissionDeniedException e) {
        exception = e;
        result = Result.PERMISSION_DENIED;
    } catch (DownloadException e) {
        exception = e;
        result = Result.FAILURE;
    } catch (Exception e) {
        exception = e;
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(rbc);
    }
}

From source file:net.technicpack.launchercore.util.Download.java

@Override
@SuppressWarnings("unused")
public void run() {
    ReadableByteChannel rbc = null;
    FileOutputStream fos = null;//  w w  w .ja va 2  s. com
    try {
        HttpURLConnection conn = Utils.openHttpConnection(url);
        int response = conn.getResponseCode();
        int responseFamily = response / 100;

        if (responseFamily == 3) {
            throw new DownloadException(
                    "The server issued a redirect response which Technic failed to follow.");
        } else if (responseFamily != 2) {
            throw new DownloadException("The server issued a " + response + " response code.");
        }

        InputStream in = getConnectionInputStream(conn);

        size = conn.getContentLength();
        outFile = new File(outPath);
        outFile.delete();

        rbc = Channels.newChannel(in);
        fos = new FileOutputStream(outFile);

        stateChanged();

        Thread progress = new MonitorThread(Thread.currentThread(), rbc);
        progress.start();

        fos.getChannel().transferFrom(rbc, 0, size > 0 ? size : Integer.MAX_VALUE);
        in.close();
        rbc.close();
        progress.interrupt();
        if (size > 0) {
            if (size == outFile.length()) {
                result = Result.SUCCESS;
            }
        } else {
            result = Result.SUCCESS;
        }
    } catch (PermissionDeniedException e) {
        exception = e;
        result = Result.PERMISSION_DENIED;
    } catch (DownloadException e) {
        exception = e;
        result = Result.FAILURE;
    } catch (Exception e) {
        exception = e;
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(rbc);
    }
}