001/* 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2026, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v2.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014 015package ch.qos.logback.core.util; 016 017import ch.qos.logback.core.Context; 018import ch.qos.logback.core.spi.ContextAwareBase; 019 020/** 021 * Allows masking of interrupt flag if previously the flag is already set. Does 022 * nothing otherwise. 023 * 024 * Typical use: 025 * 026 * <pre> 027 * InterruptUtil interruptUtil = new InterruptUtil(context); 028 * 029 * try { 030 * interruptUtil.maskInterruptFlag(); 031 * someOtherThread.join(delay); 032 * } catch (InterruptedException e) { 033 * // reachable only if join does not succeed within delay. 034 * // Without the maskInterruptFlag() call, the join() would have returned 035 * // immediately 036 * // had the current thread been interrupted previously, i.e. before entering 037 * // the above block 038 * } finally { 039 * interruptUtil.unmaskInterruptFlag(); 040 * } 041 * </pre> 042 * 043 * @author Ceki Gulcu 044 * @since 1.2.2 045 */ 046public class InterruptUtil extends ContextAwareBase { 047 048 final boolean previouslyInterrupted; 049 050 public InterruptUtil(Context context) { 051 super(); 052 setContext(context); 053 previouslyInterrupted = Thread.currentThread().isInterrupted(); 054 } 055 056 public void maskInterruptFlag() { 057 if (previouslyInterrupted) { 058 Thread.interrupted(); 059 } 060 } 061 062 public void unmaskInterruptFlag() { 063 if (previouslyInterrupted) { 064 try { 065 Thread.currentThread().interrupt(); 066 } catch (SecurityException se) { 067 addError("Failed to interrupt current thread", se); 068 } 069 } 070 } 071 072}