spring boot in multi-threaded programming and concurrent implementation


  Spring be achieved through multi-threaded programming and concurrent task executor TaskExecutor.
Use ThreadPoolTaskExecutor can be achieved based on a thread pool TaskExecutor.
Because the actual development tasks in general is asynchronous (ie non-blocking), so @EnableAsync class configuration and use @Async Bean method in the practical implementation of this is to declare an asynchronous method.

Configuration class implementation:
@Configuration 
@ComponentScan ( "com.prac.spring.task_executor")
@EnableAsync // enables asynchronous tasks that support
public class TaskExecutorConfig the implements AsyncConfigurer {
// configuration class inherited AsyncConfigurer interfaces and rewrite getAsyncExecutor method, and return the ThreadPoolTaskExecutor is,
// so we is obtained based on a thread pool of TaskExecutor
@Override
public getAsyncExecutor the Executor () {
the ThreadPoolTaskExecutor is the ThreadPoolTaskExecutor is, taskExecutor new new = ();
taskExecutor.setCorePoolSize (10);
taskExecutor.setMaxPoolSize (15);
taskExecutor.setQueueCapacity (30);
taskExecutor.initialize () ;
return, taskExecutor;
}

    / ** 
* AsyncUncaughtExceptionHandler: to handle uncaught exceptions thrown from the asynchronous method
     * An asynchronous method usually returns a Future instance that gives access to the underlying exception. When the method does not provide that return type, this handler can be used to managed such uncaught exceptions.
*/
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler(){
return null;

}

}

Guess you like

Origin www.cnblogs.com/brucehan/p/11038711.html