Skip to content

Commit

Permalink
avoid ConcurrentModificationException even adjust TTL instances in …
Browse files Browse the repository at this point in the history
…biz lifecycle callbacks #293

biz lifecycle callbacks:

- TransmittableThreadLocal#beforeExecute
- TransmittableThreadLocal#afterExecute
  • Loading branch information
oldratlee committed Sep 28, 2021
1 parent e5b4502 commit c62c31b
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/main/java/com/alibaba/ttl/TransmittableThreadLocal.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,11 @@ private void removeThisFromHolder() {
}

private static void doExecuteCallback(boolean isBefore) {
for (TransmittableThreadLocal<Object> threadLocal : holder.get().keySet()) {
// copy TTL Instances to avoid `ConcurrentModificationException`
// even adjust TTL instances in biz lifecycle callbacks(beforeExecute/afterExecute)
WeakHashMap<TransmittableThreadLocal<Object>, ?> ttlInstances = new WeakHashMap<TransmittableThreadLocal<Object>, Object>(holder.get());

for (TransmittableThreadLocal<Object> threadLocal : ttlInstances.keySet()) {
try {
if (isBefore) threadLocal.beforeExecute();
else threadLocal.afterExecute();
Expand Down

0 comments on commit c62c31b

Please sign in to comment.