List of usage examples for java.lang Thread interrupt
public void interrupt()
From source file:com.kdab.daytona.Logger.java
@Override public void destroy() { m_logger.close(); for (Thread i : m_workers) i.interrupt(); }
From source file:net.nicholaswilliams.java.licensing.licensor.interfaces.cli.TestPeriods.java
@Test public void testRun02() throws InterruptedException { Thread thread = new Thread(this.periods); thread.start();//from w w w .j ava 2 s. c o m Thread.sleep(200); thread.interrupt(); byte[] bytes = this.outputStream.toByteArray(); assertTrue("The length is not correct.", bytes.length >= 6); assertArrayEquals("The arrays are not correct.", Arrays.copyOf(bytes, 6), new byte[] { '.', '.', '.', '.', '.', '.' }); }
From source file:uk.bl.wa.util.TimeLimiter.java
/** * Creates a Thread with runnable and starts it. Waits at most timeoutMS for it to finish before sending an * interrupt. If waitAfterInterrupt is true, it then waits at most timeoutMS for the thread to finish. * * Important: Due to the nature of Java Threads the runnable might still be executing in the background after * this method has returned. There is not hard shutdown of the runnable. * @param runnable the job to run in a limited amount of time. * @param timeoutMS the amount of time to wait for processing to finish. * @param waitAfterInterrupt if true, there will be a new timeout after interrupt has been called. * @return true if the runnable finished within the timeout, else false. */// w w w .jav a2 s . c o m public static boolean run(Runnable runnable, long timeoutMS, boolean waitAfterInterrupt) { Thread parseThread = new Thread(runnable, "timelimiter_" + Long.toString(System.currentTimeMillis())); parseThread.setDaemon(true); // Ensure that the JVM will not hang on exit, waiting for Threads to finish final long startTime = System.currentTimeMillis(); log.debug("Starting timelimited run of " + runnable.getClass() + " with timeout " + timeoutMS + "ms"); parseThread.start(); try { parseThread.join(timeoutMS); } catch (InterruptedException e) { throw new RuntimeException("The Thread for the Runnable " + runnable.getClass() + " was interrupted while waiting for result", e); } long spendTime = System.currentTimeMillis() - startTime; if (spendTime <= timeoutMS) { // Finished within the timeout log.debug("Finished timelimited run of " + runnable.getClass() + " with timeout " + timeoutMS + "ms successfully in " + spendTime + "ms"); return true; } // Did not finish. Try interrupting parseThread.interrupt(); if (waitAfterInterrupt) { try { parseThread.join(timeoutMS); } catch (InterruptedException e) { throw new RuntimeException( "The Thread for the Runnable was interrupted while waiting for result after interrupting", e); } } log.debug("Finished timelimited run of " + runnable.getClass() + " with timeout " + timeoutMS + "ms unsuccessfully in " + spendTime + "ms. The created Thread is still alive"); return false; }
From source file:Main.java
public static String runScript(String script) { String sRet = ""; try {/*from w w w. j a v a2 s .c om*/ final Process m_process = Runtime.getRuntime().exec(script); final StringBuilder sbread = new StringBuilder(); Thread tout = new Thread(new Runnable() { public void run() { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(m_process.getInputStream()), 8192); String ls_1 = null; try { while ((ls_1 = bufferedReader.readLine()) != null) { sbread.append(ls_1).append("\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }); tout.start(); final StringBuilder sberr = new StringBuilder(); Thread terr = new Thread(new Runnable() { public void run() { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(m_process.getErrorStream()), 8192); String ls_1 = null; try { while ((ls_1 = bufferedReader.readLine()) != null) { sberr.append(ls_1).append("\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }); terr.start(); // int retvalue = m_process.waitFor(); while (tout.isAlive()) { Thread.sleep(50); } if (terr.isAlive()) terr.interrupt(); String stdout = sbread.toString(); String stderr = sberr.toString(); sRet = stdout + stderr; } catch (Exception e) { e.printStackTrace(); return null; } return sRet; }
From source file:Main.java
public static String runScript(String script) { String sRet = ""; try {//from www . j ava 2 s . co m final Process m_process = Runtime.getRuntime().exec(script); final StringBuilder sbread = new StringBuilder(); Thread tout = new Thread(new Runnable() { public void run() { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(m_process.getInputStream()), 8192); String ls_1 = null; try { while ((ls_1 = bufferedReader.readLine()) != null) { sbread.append(ls_1).append("\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }); tout.start(); final StringBuilder sberr = new StringBuilder(); Thread terr = new Thread(new Runnable() { public void run() { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(m_process.getErrorStream()), 8192); String ls_1 = null; try { while ((ls_1 = bufferedReader.readLine()) != null) { sberr.append(ls_1).append("\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }); terr.start(); int retvalue = m_process.waitFor(); while (tout.isAlive()) { Thread.sleep(50); } if (terr.isAlive()) terr.interrupt(); String stdout = sbread.toString(); String stderr = sberr.toString(); sRet = stdout + stderr; } catch (Exception e) { e.printStackTrace(); return null; } return sRet; }
From source file:com.sworddance.taskcontrol.SingletonTaskControlFactory.java
public void shutdown() { TaskControl taskControl = getTaskControl(); if (taskControl != null) { taskControl.shutdownNow();/*from w w w. j a v a 2 s . com*/ } if (taskControlThreadRef != null) { Thread taskControlThread = this.taskControlThreadRef.get(); if (taskControlThread != null) { taskControlThread.interrupt(); } } }
From source file:com.gsoc.ijosa.liquidgalaxycontroller.PW.collection.PwsClient.java
/** * Cancel all current HTTP requests.//from w w w . j a v a 2 s .c o m */ public void cancelAllRequests() { for (Thread thread : mThreads) { thread.interrupt(); } mThreads.clear(); }
From source file:org.apache.hadoop.mapred.JobInitializer.java
void terminate() { running = false; for (Thread t : workerThreads) { t.interrupt(); } }
From source file:bjerne.gallery.service.impl.VideoConversionServiceImpl.java
@PreDestroy public synchronized void shutdown() { LOG.info("Shutdown called. Number of currently active processes: {}", activeThreads.size()); for (Thread oneThread : activeThreads) { oneThread.interrupt(); }//from w ww . j av a2 s . c o m try { wait(5000); } catch (InterruptedException ie) { } }
From source file:org.apache.hadoop.hbase.ipc.RpcExecutor.java
public void stop() { running = false; for (Thread handler : handlers) { handler.interrupt(); } }