From 94f50e785ee122b1f31310a5b6f518990841fda6 Mon Sep 17 00:00:00 2001 From: Mona Mohamadinia Date: Tue, 11 Aug 2020 22:50:22 +0430 Subject: [PATCH] ThreadLocals and Thread Pools (#9592) --- .../ThreadLocalAwareThreadPool.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 core-java-modules/core-java-concurrency-advanced/src/main/java/com/baeldung/threadlocal/ThreadLocalAwareThreadPool.java diff --git a/core-java-modules/core-java-concurrency-advanced/src/main/java/com/baeldung/threadlocal/ThreadLocalAwareThreadPool.java b/core-java-modules/core-java-concurrency-advanced/src/main/java/com/baeldung/threadlocal/ThreadLocalAwareThreadPool.java new file mode 100644 index 0000000000..5a41cd9dbf --- /dev/null +++ b/core-java-modules/core-java-concurrency-advanced/src/main/java/com/baeldung/threadlocal/ThreadLocalAwareThreadPool.java @@ -0,0 +1,25 @@ +package com.baeldung.threadlocal; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.RejectedExecutionHandler; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +public class ThreadLocalAwareThreadPool extends ThreadPoolExecutor { + + public ThreadLocalAwareThreadPool(int corePoolSize, + int maximumPoolSize, + long keepAliveTime, + TimeUnit unit, + BlockingQueue workQueue, + ThreadFactory threadFactory, + RejectedExecutionHandler handler) { + super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler); + } + + @Override + protected void afterExecute(Runnable r, Throwable t) { + // Call remove on each ThreadLocal + } +}