List of usage examples for java.lang Thread interrupt
public void interrupt()
From source file:com.microsoft.tfs.util.thread.ThreadInterruptTimer.java
@Override public void run() { reset();//from w w w . ja v a2 s .c o m while (true) { long lastResetCopy; long interruptTimeoutSecondsCopy; synchronized (this) { lastResetCopy = lastReset; interruptTimeoutSecondsCopy = interruptTimeoutSeconds; } final long idleMilliseconds = System.currentTimeMillis() - lastResetCopy; if (idleMilliseconds > interruptTimeoutSecondsCopy * 1000) { break; } final String messageFormat = "Idle for an acceptable {0} milliseconds out of {1}"; //$NON-NLS-1$ final String message = MessageFormat.format(messageFormat, idleMilliseconds, interruptTimeoutSecondsCopy * 1000); log.trace(message); try { Thread.sleep(TIMEOUT_CHECK_PERIOD_SECONDS * 1000); } catch (final InterruptedException e) { break; } } /* * To avoid deadlock, we don't hold a lock on this object while * interrupting. The implementation of interrupt on the other thread may * call back into this class (though there's probably no reason to). */ Thread interruptThisThread; synchronized (this) { interruptThisThread = threadToInterrupt; } final String messageFormat = "Timeout period of {0} seconds exceeded, interrupting {1}"; //$NON-NLS-1$ final String message = MessageFormat.format(messageFormat, interruptTimeoutSeconds, interruptThisThread.toString()); log.debug(message); interruptThisThread.interrupt(); }
From source file:net.sourceforge.vulcan.jabber.SmackKeepAliveThreadInterrupter.java
public void interrupt() { final ThreadGroup group = Thread.currentThread().getThreadGroup(); final Thread[] threads = new Thread[group.activeCount()]; group.enumerate(threads);/*ww w . j a v a2s .co m*/ for (Thread thread : threads) { if (!thread.getName().startsWith("Smack Keep Alive")) { continue; } if (!thread.getContextClassLoader().equals(getClass().getClassLoader())) { // only wake up threads from our own class loader LOG.info("Not waking up " + thread.getName() + " because it uses a different class loader."); continue; } LOG.info("Interrupting " + thread.getName()); thread.interrupt(); try { thread.join(1000); } catch (InterruptedException ignore) { } if (thread.isAlive()) { LOG.error("Smack Keep Alive thread still alive after interruption."); } } }
From source file:com.diablominer.DiabloMiner.DiabloMiner.java
public void halt() { running.set(false);//from w w w . ja v a 2 s .c om for (int i = 0; i < threads.size(); i++) { Thread thread = threads.get(i); if (thread != Thread.currentThread()) thread.interrupt(); } }
From source file:com.xpn.xwiki.plugin.graphviz.GraphVizPlugin.java
/** * Executes GraphViz, writes the resulting image (in the requested format) in a temporary file on disk, and returns * the generated content from that file. * /*from w w w . j a v a 2s . c om*/ * @param hashCode the hascode of the content, to be used as the temporary file name * @param content the dot source code * @param extension the output file extension * @param dot which engine to execute: {@code dot} if {@code true}, {@code neato} if {@code false} * @return the content of the generated file * @throws IOException if writing the input or output files to the disk fails, or if writing the response body fails */ private byte[] getDotImage(int hashCode, String content, String extension, boolean dot) throws IOException { File dfile = getTempFile(hashCode, "input.dot", dot); if (!dfile.exists()) { FileUtils.write(dfile, content, XWiki.DEFAULT_ENCODING); } File ofile = getTempFile(hashCode, extension, dot); if (!ofile.exists()) { Runtime rt = Runtime.getRuntime(); String[] command = new String[5]; command[0] = dot ? this.dotPath : this.neatoPath; command[1] = "-T" + extension; command[2] = dfile.getAbsolutePath(); command[3] = "-o"; command[4] = ofile.getAbsolutePath(); Process p = rt.exec(command); int exitValue = -1; final Thread thisThread = Thread.currentThread(); Thread t = new Thread(new Hangcheck(thisThread), "dot-hangcheck"); t.run(); try { exitValue = p.waitFor(); t.interrupt(); } catch (InterruptedException ex) { p.destroy(); LOGGER.error("Timeout while generating image from dot", ex); } if (exitValue != 0) { LOGGER.error("Error while generating image from dot: " + IOUtils.toString(p.getErrorStream(), XWiki.DEFAULT_ENCODING)); } } return FileUtils.readFileToByteArray(ofile); }
From source file:com.opengamma.engine.cache.BerkeleyDBValueIdentifierMapTest.java
@Test(timeOut = 30000) public void interruptThread() throws Throwable { final ExecutorService threads = Executors.newSingleThreadExecutor(); try {//from w w w.java2s. c o m final Thread main = Thread.currentThread(); final Runnable interrupter = new Runnable() { @Override public void run() { try { Thread.sleep(1000); main.interrupt(); } catch (InterruptedException e) { throw new OpenGammaRuntimeException("Interrupted", e); } } }; threads.submit(interrupter); int count = 0; do { try { getPerformanceTest(); } catch (OpenGammaRuntimeException e) { assertEquals("Interrupted", e.getMessage()); count++; if (count <= 5) { threads.submit(interrupter); } else { break; } } } while (true); } finally { threads.shutdown(); Thread.interrupted(); threads.awaitTermination(5, TimeUnit.SECONDS); } }
From source file:org.diorite.impl.metrics.Metrics.java
public void stop() { final Thread t = this.thread; this.thread = null; t.interrupt(); }
From source file:org.archive.modules.writer.WARCWriterProcessorTest.java
/** * test if {@link WARCWriterProcessor} recovers on I/O error. *//*ww w. j a v a2 s . c om*/ public void testResilientOnError() throws Exception { // override setupPool() to use test version of WARCWriter. final WARCWriterProcessor wwp = new WARCWriterProcessor() { protected void setupPool(AtomicInteger serialNo) { setPool(new TestWriterPool(this, 1)); } }; wwp.start(); final CrawlURI curi = new CrawlURI(UURIFactory.getInstance("http://test.com/")); // necessary to pass shouldProcess() test. curi.setFetchStatus(200); curi.setContentSize(1); // necessary to pass shouldWrite() test. curi.setFetchType(FetchType.HTTP_GET); // make a first call. FailWARCWriter throws an IOException // upon first call to getPosition() - this situation can be // easily overlooked as method name does not suggest it's // writing anything to disk. wwp.process(curi); Collection<Throwable> failures1 = curi.getNonFatalFailures(); assertEquals(1, failures1.size()); // make second call. if the exception during previous call // caused any inconsistency, most likely outcome is second // call never returns. final Thread me = Thread.currentThread(); Thread th = new Thread() { public void run() { // WARCWriterProcessor#process() will never // throw InterruptedException try { wwp.process(curi); // let parent thread know I'm done! me.interrupt(); Thread.sleep(500); } catch (InterruptedException ex) { } }; }; th.start(); // wait 5 seconds for th to finish. it should not // take this long to finish. try { th.join(5000); } catch (InterruptedException ex) { // ok, th finished return; } fail("second process() call got blocked too long"); }
From source file:com.brienwheeler.lib.test.stepper.SteppableThreadTest.java
private void breakBarrier(CyclicBarrier barrier) throws BrokenBarrierException { final Thread testThread = Thread.currentThread(); Thread interruptThread = new Thread() { @Override/*from w w w .java 2s .c o m*/ public void run() { try { Thread.sleep(10L); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } testThread.interrupt(); } }; interruptThread.start(); try { barrier.await(); Assert.fail(); } catch (InterruptedException e) { } }
From source file:com.brienwheeler.lib.test.stepper.SteppableThreadTest.java
private void interruptInWait(SteppableThread stepper, TestMode mode) throws InterruptedException { final Thread testThread = Thread.currentThread(); Thread interruptThread = new Thread() { @Override/* w ww. j av a 2 s . c o m*/ public void run() { try { Thread.sleep(10L); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } testThread.interrupt(); } }; interruptThread.start(); try { if (mode == TestMode.RELEASE) stepper.release(); else if (mode == TestMode.WAITDONE) stepper.waitDone(); else if (mode == TestMode.JOIN) stepper.releaseAndJoin(); Assert.fail(); } catch (RuntimeException e) { Assert.assertTrue(Thread.interrupted()); // test and clear Assert.assertEquals(InterruptedException.class, e.getCause().getClass()); } interruptThread.join(); }
From source file:org.ambientdynamix.contextplugins.withingsdataplugin.WithingsDataPluginRuntime.java
@Override public void stop() { okToRun = false;/*w w w . j a v a2s .c om*/ Thread t = Thread.currentThread(); t.interrupt(); Log.d(TAG, "Stopped!"); Log.d("Withings", "Stopped!111"); }