MDCUtil { public static MDC.MDCCloseable safePutCloseable(String key, String value) { try { return MDC.putCloseable(key, value); } catch (Exception e) { log.error("Failed to put MDC", e); return null; } } private  MDCUtil(); static Executor withMDC(); static Executor withMDC(Executor executor); static Runnable withMDC(Runnable r); static Callable<V> withMDC(Callable<V> r); static MDC.MDCCloseable safePutCloseable(String key, String value);  }
@Test @Repeat(times = 10000, threads = 4) public void safePutCloseable() { try (MDCCloseable ignored = MDCUtil.safePutCloseable("foo", "bar")) { assertThat(MDC.get("foo"), is("bar")); } assertThat(MDC.get("foo"), is(nullValue())); }