/* * @see DATAREDIS-415 */ @Test public void interruptAtStart() throws Exception { final Thread main = Thread.currentThread(); // interrupt thread once Executor.execute is called doAnswer( new Answer<Object>() { @Override public Object answer(final InvocationOnMock invocationOnMock) throws Throwable { main.interrupt(); return null; } }) .when(executorMock) .execute(any(Runnable.class)); container.addMessageListener(adapter, new ChannelTopic("a")); container.start(); // reset the interrupted flag to not destroy the teardown assertThat(Thread.interrupted(), is(true)); assertThat(container.isRunning(), is(false)); }
public static void main(String[] args) throws IOException { final File socketFile = new File(new File(System.getProperty("java.io.tmpdir")), "junixsocket-test.sock"); AFUNIXServerSocket server = AFUNIXServerSocket.newInstance(); server.bind(new AFUNIXSocketAddress(socketFile)); System.out.println("server: " + server); while (!Thread.interrupted()) { System.out.println("Waiting for connection..."); Socket sock = server.accept(); System.out.println("Connected: " + sock); InputStream is = sock.getInputStream(); OutputStream os = sock.getOutputStream(); System.out.println("Saying hello to client " + os); os.write("Hello, dear Client".getBytes()); os.flush(); byte[] buf = new byte[128]; int read = is.read(buf); System.out.println("Client's response: " + new String(buf, 0, read)); os.close(); is.close(); sock.close(); } }
public void fiveSecondTimeoutAndInterruptible(int timeoutMs) { Thread.interrupted(); try { Thread.sleep(timeoutMs); } catch (InterruptedException e) { interrupted.set(true); } }
/** @see java.lang.Runnable#run() */ public void run() { final Thread thread = Thread.currentThread(); while (!Thread.interrupted()) { m_set.add(m_touchable); m_list.add(m_touchable); try { synchronized (thread) { thread.wait(); } } catch (InterruptedException e) { // propagate interrupt thread.interrupt(); } } }
@Override public void run() { try { while (!Thread.interrupted()) { try { setupSocket(hostname, portNumber); } catch (IOException e1) { Thread.sleep(rand.nextInt(1000)); continue; } int numAttempts = rand.nextInt(10); for (int i = 0; i < numAttempts; i++) { boolean doClose = i == numAttempts - 1; try { if (tryConnect(hostname, doClose) == false) { fails++; } else { oks++; } Thread.sleep(rand.nextInt(10)); } catch (IOException e) { System.err.println(e.toString()); // java.net.ConnectException: Cannot assign requested address // https://groups.google.com/forum/#!topic/jvm-languages/E-8QyckBmf4 exceptions++; } catch (InterruptedException e) { break; } } try { shutdownSocket(); } catch (IOException e) { } } } catch (InterruptedException e) { // done } }
@After public void cleanup() { // Clear interrupted state Thread.interrupted(); }