Skip to content

Commit 5d6c477

Browse files
ramansahasimaibin
authored andcommitted
Logged InterruptedException instead of ignoring it (eugenp#3517)
1 parent 2332bc1 commit 5d6c477

4 files changed

Lines changed: 58 additions & 48 deletions

File tree

‎core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Data.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.baeldung.concurrent.waitandnotify;
22

3+
import org.slf4j.Logger;
4+
35
public class Data {
46
private String packet;
57

@@ -11,7 +13,9 @@ public synchronized String receive() {
1113
while (transfer) {
1214
try {
1315
wait();
14-
} catch (InterruptedException e) {}
16+
} catch (InterruptedException e) {
17+
System.out.println("Thread Interrupted");
18+
}
1519
}
1620
transfer = true;
1721

@@ -23,7 +27,9 @@ public synchronized void send(String packet) {
2327
while (!transfer) {
2428
try {
2529
wait();
26-
} catch (InterruptedException e) {}
30+
} catch (InterruptedException e) {
31+
System.out.println("Thread Interrupted");
32+
}
2733
}
2834
transfer = false;
2935

‎core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Receiver.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public void run() {
1919
//Thread.sleep() to mimic heavy server-side processing
2020
try {
2121
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000));
22-
} catch (InterruptedException e) {}
22+
} catch (InterruptedException e) {
23+
System.out.println("Thread Interrupted");
24+
}
2325
}
2426
}
2527
}

‎core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Sender.java‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ public Sender(Data data) {
1111

1212
public void run() {
1313
String packets[] = {
14-
"First packet",
15-
"Second packet",
16-
"Third packet",
17-
"Fourth packet",
18-
"End"
14+
"First packet",
15+
"Second packet",
16+
"Third packet",
17+
"Fourth packet",
18+
"End"
1919
};
2020

2121
for (String packet : packets) {
@@ -24,7 +24,9 @@ public void run() {
2424
//Thread.sleep() to mimic heavy server-side processing
2525
try {
2626
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000));
27-
} catch (InterruptedException e) {}
27+
} catch (InterruptedException e) {
28+
System.out.println("Thread Interrupted");
29+
}
2830
}
2931
}
3032
}

‎core-java-concurrency/src/test/java/com/baeldung/concurrent/waitandnotify/NetworkIntegrationTest.java‎

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,38 @@
1313

1414
public class NetworkIntegrationTest {
1515

16-
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
17-
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
18-
private String expected;
19-
20-
@Before
21-
public void setUpStreams() {
22-
System.setOut(new PrintStream(outContent));
23-
System.setErr(new PrintStream(errContent));
24-
}
25-
26-
@Before
27-
public void setUpExpectedOutput() {
28-
StringWriter expectedStringWriter = new StringWriter();
29-
30-
PrintWriter printWriter = new PrintWriter(expectedStringWriter);
31-
printWriter.println("First packet");
32-
printWriter.println("Second packet");
33-
printWriter.println("Third packet");
34-
printWriter.println("Fourth packet");
35-
printWriter.close();
36-
37-
expected = expectedStringWriter.toString();
38-
}
16+
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
17+
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
18+
private String expected;
19+
20+
@Before
21+
public void setUpStreams() {
22+
System.setOut(new PrintStream(outContent));
23+
System.setErr(new PrintStream(errContent));
24+
}
25+
26+
@Before
27+
public void setUpExpectedOutput() {
28+
StringWriter expectedStringWriter = new StringWriter();
29+
30+
PrintWriter printWriter = new PrintWriter(expectedStringWriter);
31+
printWriter.println("First packet");
32+
printWriter.println("Second packet");
33+
printWriter.println("Third packet");
34+
printWriter.println("Fourth packet");
35+
printWriter.close();
36+
37+
expected = expectedStringWriter.toString();
38+
}
3939

40-
@After
41-
public void cleanUpStreams() {
42-
System.setOut(null);
43-
System.setErr(null);
44-
}
45-
46-
@Test
47-
public void givenSenderAndReceiver_whenSendingPackets_thenNetworkSynchronized() {
40+
@After
41+
public void cleanUpStreams() {
42+
System.setOut(null);
43+
System.setErr(null);
44+
}
45+
46+
@Test
47+
public void givenSenderAndReceiver_whenSendingPackets_thenNetworkSynchronized() {
4848
Data data = new Data();
4949
Thread sender = new Thread(new Sender(data));
5050
Thread receiver = new Thread(new Receiver(data));
@@ -54,12 +54,12 @@ public void givenSenderAndReceiver_whenSendingPackets_thenNetworkSynchronized()
5454

5555
//wait for sender and receiver to finish before we test against expected
5656
try {
57-
sender.join();
58-
receiver.join();
59-
} catch (InterruptedException e) {
60-
e.printStackTrace();
61-
}
62-
63-
assertEquals(expected, outContent.toString());
64-
}
57+
sender.join();
58+
receiver.join();
59+
} catch (InterruptedException e) {
60+
e.printStackTrace();
61+
}
62+
63+
assertEquals(expected, outContent.toString());
64+
}
6565
}

0 commit comments

Comments
 (0)