The preparatory work

  • See my previous article “ThreadLocal delivery in Different threads” for more details.

1) Introduce dependencies

<! -- https://mvnrepository.com/artifact/com.alibaba/transmittable-thread-local -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>transmittable-thread-local</artifactId>
            <version>2.114.</version>
        </dependency>
Copy the code
  1. Add thread pool configuration
import com.alibaba.ttl.threadpool.TtlExecutors;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/ * * *@author miao
 */
@Configuration
public class TaskPoolConfig {
    /** * Set the name of the Bean without setting it, there is no way to configure information in the task **@return Executor
     */
    @Bean("taskExecutor")
    public Executor taskExecutor(a) {

        // Create a build thread pool from ThreadPoolTaskExecutor
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // Set the initial number of threads to 5
        executor.setCorePoolSize(5);
        // Set the maximum number of threads to 10
        executor.setMaxPoolSize(10);
        // Set the maximum number of tasks for the task queue
        executor.setQueueCapacity(200);
        // Set the elapsed time of the initialization thread to 60 seconds
        // If the number of existing threads exceeds 5, the destruction time of the excess idle threads is set to 60 seconds
        executor.setKeepAliveSeconds(60);
        // Set the thread prefix
        executor.setThreadNamePrefix("taskExecutor-");
        // The thread pool saturation policy I set here is the callerRun policy, i.e., the task is executed by the thread of the caller
        AbortPolicy: Throws an exception directly. This is the default policy.
        //CallerRunsPolicy: execute the task with the caller's thread;
        DiscardOldestPolicy: Discards the top task in the blocking queue and executes the current task.
        //DiscardPolicy: discards tasks directly;
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // Sets whether to wait for the task to complete when closing the thread pool
        executor.setWaitForTasksToCompleteOnShutdown(true);
        // Sets the number of seconds to wait to terminate
        executor.setAwaitTerminationSeconds(60);
        // Returns the thread pool that has been set up
        returnTtlExecutors.getTtlExecutor(executor); }}Copy the code
  1. AsyncService
    @Async("taskExecutor")
    public void getThreadLocalValue(a) {

        System.out.println("+ + + + + + + + + + + + + + +" + ThreadLocalUtil.getValue());
    }
Copy the code
  1. ThreadLocalUtilIn theLOCK_FLAGImplementation class changed toTransmittableThreadLocal
private static final ThreadLocal<String> LOCK_FLAG = new TransmittableThreadLocal<>();
Copy the code
  1. Browser access:http://localhost:8080/test

The result is multiple accesses and the values remain the same