Logged InterruptedException instead of ignoring it (#3517)

This commit is contained in:
ramansahasi 2018-01-26 23:34:38 +05:30 committed by maibin
parent 2332bc1a14
commit 5d6c47789e
4 changed files with 58 additions and 48 deletions

View File

@ -1,5 +1,7 @@
package com.baeldung.concurrent.waitandnotify; package com.baeldung.concurrent.waitandnotify;
import org.slf4j.Logger;
public class Data { public class Data {
private String packet; private String packet;
@ -11,7 +13,9 @@ public class Data {
while (transfer) { while (transfer) {
try { try {
wait(); wait();
} catch (InterruptedException e) {} } catch (InterruptedException e) {
System.out.println("Thread Interrupted");
}
} }
transfer = true; transfer = true;
@ -23,7 +27,9 @@ public class Data {
while (!transfer) { while (!transfer) {
try { try {
wait(); wait();
} catch (InterruptedException e) {} } catch (InterruptedException e) {
System.out.println("Thread Interrupted");
}
} }
transfer = false; transfer = false;

View File

@ -19,7 +19,9 @@ public class Receiver implements Runnable {
//Thread.sleep() to mimic heavy server-side processing //Thread.sleep() to mimic heavy server-side processing
try { try {
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000)); Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000));
} catch (InterruptedException e) {} } catch (InterruptedException e) {
System.out.println("Thread Interrupted");
}
} }
} }
} }

View File

@ -11,11 +11,11 @@ public class Sender implements Runnable {
public void run() { public void run() {
String packets[] = { String packets[] = {
"First packet", "First packet",
"Second packet", "Second packet",
"Third packet", "Third packet",
"Fourth packet", "Fourth packet",
"End" "End"
}; };
for (String packet : packets) { for (String packet : packets) {
@ -24,7 +24,9 @@ public class Sender implements Runnable {
//Thread.sleep() to mimic heavy server-side processing //Thread.sleep() to mimic heavy server-side processing
try { try {
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000)); Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000));
} catch (InterruptedException e) {} } catch (InterruptedException e) {
System.out.println("Thread Interrupted");
}
} }
} }
} }

View File

@ -13,38 +13,38 @@ import org.junit.Test;
public class NetworkIntegrationTest { public class NetworkIntegrationTest {
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
private String expected; private String expected;
@Before @Before
public void setUpStreams() { public void setUpStreams() {
System.setOut(new PrintStream(outContent)); System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent)); System.setErr(new PrintStream(errContent));
} }
@Before @Before
public void setUpExpectedOutput() { public void setUpExpectedOutput() {
StringWriter expectedStringWriter = new StringWriter(); StringWriter expectedStringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(expectedStringWriter); PrintWriter printWriter = new PrintWriter(expectedStringWriter);
printWriter.println("First packet"); printWriter.println("First packet");
printWriter.println("Second packet"); printWriter.println("Second packet");
printWriter.println("Third packet"); printWriter.println("Third packet");
printWriter.println("Fourth packet"); printWriter.println("Fourth packet");
printWriter.close(); printWriter.close();
expected = expectedStringWriter.toString(); expected = expectedStringWriter.toString();
} }
@After @After
public void cleanUpStreams() { public void cleanUpStreams() {
System.setOut(null); System.setOut(null);
System.setErr(null); System.setErr(null);
} }
@Test @Test
public void givenSenderAndReceiver_whenSendingPackets_thenNetworkSynchronized() { public void givenSenderAndReceiver_whenSendingPackets_thenNetworkSynchronized() {
Data data = new Data(); Data data = new Data();
Thread sender = new Thread(new Sender(data)); Thread sender = new Thread(new Sender(data));
Thread receiver = new Thread(new Receiver(data)); Thread receiver = new Thread(new Receiver(data));
@ -54,12 +54,12 @@ public class NetworkIntegrationTest {
//wait for sender and receiver to finish before we test against expected //wait for sender and receiver to finish before we test against expected
try { try {
sender.join(); sender.join();
receiver.join(); receiver.join();
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
assertEquals(expected, outContent.toString()); assertEquals(expected, outContent.toString());
} }
} }