Skip to content

Commit a0979fc

Browse files
committedMar 28, 2019
Fix non-finishing UsingConcurrentCollections method
1 parent 20b9e8d commit a0979fc

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed
 

‎src/main/java/br/com/leonardoz/features/collections/UsingConcurrentCollections.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ public static void usingBlockingQueue() {
155155

156156
} catch (InterruptedException e) {
157157
// interrupted pattern
158+
// InterruptedException makes isInterrupted returns false
159+
Thread.currentThread().interrupt();
158160
System.err.println("Consumer Finished");
159161
}
160162
}
@@ -181,7 +183,7 @@ public static void usingBlockingQueue() {
181183
}
182184
};
183185

184-
// Multiple producers - Examples using simple threads this time.
186+
// Multiple producers - Examples using simple threads this time.
185187
Thread producer1 = new Thread(runProducer);
186188
producer1.start();
187189
Thread producer2 = new Thread(runProducer);
@@ -204,8 +206,8 @@ public static void usingBlockingQueue() {
204206
}
205207

206208
public static void main(String[] args) {
207-
usingConcurrentHashMap();
208-
usingCopyOnWriteArrayList();
209+
// usingConcurrentHashMap();
210+
// usingCopyOnWriteArrayList();
209211
usingBlockingQueue();
210212
}
211213

‎src/main/java/br/com/leonardoz/patterns/task_cancel/ThreadTaskCancel.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class ThreadTaskCancel {
1919
private Thread thread;
2020
private Runnable task = () -> {
2121
while (!Thread.currentThread().isInterrupted()) {
22-
// keep going
22+
// keep going - be aware of using this Pattern with the Interrupted exception! It won't work.
2323
}
2424
};
2525

0 commit comments

Comments
 (0)