View Javadoc
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 nothing otherwise.
8    * 
9    * Typical use:
10   * 
11   * <pre>
12   * InterruptUtil interruptUtil = new InterruptUtil(context);
13   * 
14   * try {
15   *   interruptUtil.maskInterruptFlag();
16   *   someOtherThread.join(delay); 
17   * } catch(InterruptedException e) {
18   *   // reachable only if join does not succeed within delay.
19   *   // Without the maskInterruptFlag() call, the join() would have returned immediately
20   *   // had the current thread been interrupted previously, i.e. before entering the above block    
21   * } finally {
22   *   interruptUtil.unmaskInterruptFlag();
23   * }
24   * </pre>
25   * @author Ceki Gulcu
26   * @since 1.2.2
27   */
28  public class InterruptUtil extends ContextAwareBase {
29  
30      final boolean previouslyInterrupted;
31  
32      public InterruptUtil(Context context) {
33          super();
34          setContext(context);
35          previouslyInterrupted = Thread.currentThread().isInterrupted();
36      }
37  
38      public void maskInterruptFlag() {
39          if (previouslyInterrupted) {
40              Thread.interrupted();
41          }
42      }
43  
44      public void unmaskInterruptFlag() {
45          if (previouslyInterrupted) {
46              try {
47                  Thread.currentThread().interrupt();
48              } catch (SecurityException se) {
49                  addError("Failed to intrreupt current thread", se);
50              }
51          }
52      }
53  
54  }