Detail spring multithreading and timing tasks

  • 2020-06-23 00:17:54
  • OfStack

This paper describes the use of multithreading in spring and the use of timed tasks.

1. The use of spring multithreaded tasks

spring implements multithreading and concurrent programming through task executor TaskExecutor. ThreadPoolTaskExecutor is typically used to implement a thread pool-based TaskExecutor.

The first thing you need to do is implement the interface AsyncConfigurer to open up a thread pool

The code is as follows:


package com.foreveross.service.weixin.test.thread;

import java.util.concurrent.Executor;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 *  injection 1 A thread pool 
 * @author mingge
 *
 */

@Configuration
@ComponentScan("com.foreveross.service.weixin.test.thread")
@EnableAsync
public class TaskExecutorConfig implements AsyncConfigurer {

  @Override
  public Executor getAsyncExecutor() {
    ThreadPoolTaskExecutor taskExecutor=new ThreadPoolTaskExecutor();
    taskExecutor.setCorePoolSize(5);
    taskExecutor.setMaxPoolSize(20);
    taskExecutor.setQueueCapacity(25);
    taskExecutor.initialize();
    return taskExecutor;
  }

  @Override
  public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
    return null;
  }

  
}

Then inject a class, implement your business, and declare it an asynchronous task using the @Async annotation in your Bean method

The code is as follows:


package com.foreveross.service.weixin.test.thread;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 *  Thread pool task 
 * @author mingge
 *
 */
@Service
public class TaskService {

  @Async
  public void executeAsyncTask(int i){
    System.out.println(" Perform asynchronous tasks :"+i);
  }
  
  @Async
  public void executeAsyncTask1(int i){
    System.out.println(" Perform asynchronous tasks 1:"+(i+i));
  }
}

Finally, through the test, you can see that your implementation is asynchronous.


package com.foreveross.service.weixin.test.thread;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;


/**
 * 
 * @author mingge
 *
 */
public class Test {

  public static void main(String[] args) {
    AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(TaskExecutorConfig.class);
    TaskService taskService=context.getBean(TaskService.class);
    for(int i=0;i<20;i++){
      taskService.executeAsyncTask(i);
      taskService.executeAsyncTask1(i);
    }
    // And then you can see from the results that the results are concurrent rather than sequential 
    context.close();
  }
}

2. Use of spring timer tasks

In the java native ecosystem, we can use timer, where this site describes the use of timed tasks in Spring


package com.foreveross.service.weixin.test.thread;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@Configuration
@ComponentScan("com.foreveross.service.weixin.test.thread")
@EnableScheduling// Turn on timer support 
public class TaskSchedulerConfig {

}

package com.foreveross.service.weixin.test.thread;

import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class TimerTaskJob {

  @Scheduled(fixedRate=2000)
  public void test(){
    System.out.println(" I'm on a timer :"+new Date().getSeconds());
  }
}

package com.foreveross.service.weixin.test.thread;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestTimer {
  public static void main(String[] args) {
    AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(TaskSchedulerConfig.class);
    
    //context.close();
  }
}

Related articles: