@Test public void nearSimultaneousPutsShouldNotCauseConcurrentModificationException() throws InterruptedException { for (int i = 0; i < 2048; i++) { mdcAdapter.put("k" + i, "v" + i); } ChildThread childThread = new ChildThread(mdcAdapter, null, null) { @Override public void run() { for (int i = 0; i < 16; i++) { mdcAdapter.put("ck" + i, "cv" + i); Thread.yield(); } successful = true; } }; childThread.start(); Thread.sleep(1); for (int i = 0; i < 16; i++) { mdcAdapter.put("K" + i, "V" + i); } childThread.join(); assertTrue(childThread.successful); }
public void put(String key, String val) throws IllegalArgumentException { if (key == null) { throw new IllegalArgumentException("key cannot be null"); } Map<String, String> oldMap = copyOnThreadLocal.get(); Integer lastOp = getAndSetLastOperation(WRITE_OPERATION); if (wasLastOpReadOrNull(lastOp) || oldMap == null) { Map<String, String> newMap = duplicateAndInsertNewMap(oldMap); newMap.put(key, val); } else { oldMap.put(key, val); } }
LogbackMDCAdapter implements MDCAdapter { public void put(String key, String val) throws IllegalArgumentException { if (key == null) { throw new IllegalArgumentException("key cannot be null"); } Map<String, String> oldMap = copyOnThreadLocal.get(); Integer lastOp = getAndSetLastOperation(WRITE_OPERATION); if (wasLastOpReadOrNull(lastOp) || oldMap == null) { Map<String, String> newMap = duplicateAndInsertNewMap(oldMap); newMap.put(key, val); } else { oldMap.put(key, val); } } }
LogbackMDCAdapter implements MDCAdapter { public void put(String key, String val) throws IllegalArgumentException { if (key == null) { throw new IllegalArgumentException("key cannot be null"); } Map<String, String> oldMap = copyOnThreadLocal.get(); Integer lastOp = getAndSetLastOperation(WRITE_OPERATION); if (wasLastOpReadOrNull(lastOp) || oldMap == null) { Map<String, String> newMap = duplicateAndInsertNewMap(oldMap); newMap.put(key, val); } else { oldMap.put(key, val); } }  }
LogbackMDCAdapter implements MDCAdapter { public void put(String key, String val) throws IllegalArgumentException { if (key == null) { throw new IllegalArgumentException("key cannot be null"); } Map<String, String> oldMap = copyOnThreadLocal.get(); Integer lastOp = getAndSetLastOperation(WRITE_OPERATION); if (wasLastOpReadOrNull(lastOp) || oldMap == null) { Map<String, String> newMap = duplicateAndInsertNewMap(oldMap); newMap.put(key, val); } else { oldMap.put(key, val); } }  void put(String key, String val); void remove(String key); void clear(); String get(String key); Map<String, String> getPropertyMap(); Set<String> getKeys(); Map<String, String> getCopyOfContextMap(); void setContextMap(Map<String, String> contextMap); }
LogbackMDCAdapter implements MDCAdapter { public void put(String key, String val) throws IllegalArgumentException { if (key == null) { throw new IllegalArgumentException("key cannot be null"); } Map<String, String> oldMap = copyOnThreadLocal.get(); Integer lastOp = getAndSetLastOperation(WRITE_OPERATION); if (wasLastOpReadOrNull(lastOp) || oldMap == null) { Map<String, String> newMap = duplicateAndInsertNewMap(oldMap); newMap.put(key, val); } else { oldMap.put(key, val); } }  void put(String key, String val); void remove(String key); void clear(); String get(String key); Map<String, String> getPropertyMap(); Set<String> getKeys(); Map<String, String> getCopyOfContextMap(); void setContextMap(Map<String, String> contextMap);  }