1 package ch.qos.logback.core.util;
2
3 import ch.qos.logback.core.Context;
4 import ch.qos.logback.core.spi.ContextAwareBase;
5
6 /**
7 * Allows masking of interrupt flag if previously the flag is already set. Does
8 * nothing otherwise.
9 *
10 * Typical use:
11 *
12 * <pre>
13 * InterruptUtil interruptUtil = new InterruptUtil(context);
14 *
15 * try {
16 * interruptUtil.maskInterruptFlag();
17 * someOtherThread.join(delay);
18 * } catch (InterruptedException e) {
19 * // reachable only if join does not succeed within delay.
20 * // Without the maskInterruptFlag() call, the join() would have returned
21 * // immediately
22 * // had the current thread been interrupted previously, i.e. before entering
23 * // the above block
24 * } finally {
25 * interruptUtil.unmaskInterruptFlag();
26 * }
27 * </pre>
28 *
29 * @author Ceki Gulcu
30 * @since 1.2.2
31 */
32 public class InterruptUtil extends ContextAwareBase {
33
34 final boolean previouslyInterrupted;
35
36 public InterruptUtil(Context context) {
37 super();
38 setContext(context);
39 previouslyInterrupted = Thread.currentThread().isInterrupted();
40 }
41
42 public void maskInterruptFlag() {
43 if (previouslyInterrupted) {
44 Thread.interrupted();
45 }
46 }
47
48 public void unmaskInterruptFlag() {
49 if (previouslyInterrupted) {
50 try {
51 Thread.currentThread().interrupt();
52 } catch (SecurityException se) {
53 addError("Failed to interrupt current thread", se);
54 }
55 }
56 }
57
58 }