List of usage examples for java.lang Thread interrupt
public void interrupt()
From source file:com.splout.db.benchmark.TCPTest.java
public static void tcpTest(String file, String table) throws UnknownHostException, IOException, InterruptedException, JSONSerDeException { final TCPServer server = new TCPServer(8888, file, table); Thread t = new Thread() { @Override/*from w w w . j a va 2s . c o m*/ public void run() { server.serve(); } }; t.start(); while (!server.isServing()) { Thread.sleep(100); } Socket clientSocket = new Socket("localhost", 8888); DataInputStream inFromServer = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream())); try { do { // Read a record inFromServer.readUTF(); inFromServer.readInt(); inFromServer.readDouble(); inFromServer.readUTF(); } while (true); } catch (Throwable th) { th.printStackTrace(); } clientSocket.close(); server.stop(); t.interrupt(); }
From source file:org.kuali.rice.edl.framework.workflow.EDocLitePostProcessor.java
protected static void postEvent(String docId, Object event, String eventName) { try {//w w w .ja v a2 s . c o m Document doc = getEDLContent(docId); if (LOG.isDebugEnabled()) { LOG.debug("Submitting doc: " + XmlJotter.jotNode(doc)); } String urlstring = getURL(doc); if (org.apache.commons.lang.StringUtils.isEmpty(urlstring)) { LOG.warn("No eventNotificationURL defined in EDLContent"); return; } Document eventDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element eventE = eventDoc.createElement("event"); eventE.setAttribute("type", eventName); eventDoc.appendChild(eventE); Element infoE = (Element) eventDoc.importNode(propertiesToXml(event, "info"), true); Element docIdE = eventDoc.createElement("docId"); docIdE.appendChild(eventDoc.createTextNode(String.valueOf(docId))); infoE.appendChild(docIdE); eventE.appendChild(infoE); eventE.appendChild(eventDoc.importNode(doc.getDocumentElement(), true)); String query = "docId=" + docId; if (urlstring.indexOf('?') != -1) { urlstring += "&" + query; } else { urlstring += "?" + query; } final String _urlstring = urlstring; final Document _eventDoc = eventDoc; // a super cheesy way to enforce asynchronicity/timeout follows: final Thread t = new Thread(new Runnable() { public void run() { try { LOG.debug("Post Event calling url: " + _urlstring); submitURL(_urlstring, _eventDoc); LOG.debug("Post Event done calling url: " + _urlstring); } catch (Exception e) { LOG.error(e); } } }); t.setDaemon(true); t.start(); // kill the submission thread if it hasn't completed after 1 minute TIMER.schedule(new TimerTask() { public void run() { t.interrupt(); } }, SUBMIT_URL_MILLISECONDS_WAIT); } catch (Exception e) { if (e instanceof RuntimeException) { throw (RuntimeException) e; } throw new RuntimeException(e); } }
From source file:examples.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() { public void run() { int ch; try { while (!interrupted() && (ch = localInput.read()) != -1) { remoteOutput.write(ch); remoteOutput.flush(); }/*from w w w . j a v a 2 s. c o m*/ } 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.buaa.cfs.utils.Shell.java
private static void joinThread(Thread t) { while (t.isAlive()) { try {// w w w. java 2 s . c o m t.join(); } catch (InterruptedException ie) { if (LOG.isWarnEnabled()) { LOG.warn("Interrupted while joining on: " + t, ie); } t.interrupt(); // propagate interrupt } } }
From source file:org.quickserver.util.pool.thread.ThreadObjectFactory.java
public void destroyObject(Object obj) { if (obj == null) return;/*from w ww .j ava2 s .c o m*/ Thread thread = (Thread) obj; thread.interrupt(); thread = null; }
From source file:Test.java
private void start() throws InterruptedException { for (int i = 0; i < 10; i++) { inventoryMap.put("Book #" + i, 100); }/*from w ww . j a v a 2 s .c om*/ for (int i = 0; i < 20; i++) { createOrderingThread(); } checkInventoryLevels(); for (Thread thread : orderingThreads) { thread.interrupt(); } displayOrders(); }
From source file:dk.netarkivet.common.utils.ProcessUtils.java
/** Wait for the end of a process, but only for a limited time. This * method takes care of the ways waitFor can get interrupted. * * @param p Process to wait for/*from ww w . j a v a2 s .com*/ * @param maxWait The maximum number of milliseconds to wait for the * process to exit. * @return Exit value for process, or null if the process didn't exit * within the expected time. */ public static Integer waitFor(final Process p, long maxWait) { ArgumentNotValid.checkNotNull(p, "Process p"); ArgumentNotValid.checkPositive(maxWait, "long maxWait"); long startTime = System.currentTimeMillis(); Timer timer = new Timer(true); final Thread waitThread = Thread.currentThread(); boolean wakeupScheduled = false; final AtomicBoolean doneWaiting = new AtomicBoolean(false); while (System.currentTimeMillis() < startTime + maxWait) { try { if (!wakeupScheduled) { // First time in here, we need to start the wakup thread, // but be sure it doesn't notify us too early or too late. synchronized (waitThread) { timer.schedule(new TimerTask() { public void run() { synchronized (waitThread) { if (!doneWaiting.get()) { waitThread.interrupt(); } } } }, maxWait); wakeupScheduled = true; } } p.waitFor(); break; } catch (InterruptedException e) { // May happen for a number of reasons. We just check if we've // timed out yet when we go through the loop again. } } synchronized (waitThread) { timer.cancel(); doneWaiting.set(true); Thread.interrupted(); // In case the timer task interrupted. } try { return p.exitValue(); } catch (IllegalThreadStateException e) { log.warn("Process '" + p + "' did not exit within " + (System.currentTimeMillis() - startTime) + " milliseconds"); return null; } }
From source file:com.hkd.socketclient.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() { @Override// w w w .j av a 2s .c o m 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() { @Override 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:org.apache.commons.net.examples.util.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() { @Override//from ww w .ja v a2 s. c om 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() { @Override 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) { // Ignored } }
From source file:org.marketcetera.saclient.SAClientWSTest.java
private static <R> void verifyInvocationCannotBeInterrupted(final WSTester<R> inTester) throws Exception { resetServiceParameters();/*from w ww . ja v a 2s . c om*/ getMockSAService().setSleep(true); inTester.setReturnValue(false); final Semaphore sema = new Semaphore(0); final AtomicReference<Exception> interruptFailure = new AtomicReference<Exception>(); Thread t = new Thread() { @Override public void run() { sema.release(); try { inTester.invokeApi(false); } catch (Exception ex) { interruptFailure.set(ex); } } }; t.start(); //Wait for the thread to be started sema.acquire(); //Interrupt it as soon as it is found started t.interrupt(); //wait for it to end t.join(); //verify that we are not able to interrupt it assertNull("API invocation got interrupted!", interruptFailure.get()); }