Skip to content

Commit 63707fe

Browse files
author
Vitalii Cherkashyn
committed
marking Thread as interrupted
1 parent da8d834 commit 63707fe

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

thread/Interrupted.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class PrimeProducer extends Thread {
2+
private final BlockingQueue<BigInteger> queue;
3+
4+
PrimeProducer(BlockingQueue<BigInteger> queue) {
5+
this.queue = queue;
6+
}
7+
8+
public void run() {
9+
try {
10+
BigInteger p = BigInteger.ONE;
11+
// check 'interrupted' flag
12+
while (!Thread.currentThread().isInterrupted())
13+
queue.put(p = p.nextProbablePrime());
14+
} catch (InterruptedException consumed) {
15+
/* Allow thread to exit */
16+
}
17+
}
18+
19+
// mark Thread as 'interrupted'
20+
public void cancel() { interrupt(); }
21+
}

0 commit comments

Comments
 (0)