Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the asynchronous threading method of Spring

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "what is the asynchronous threading method of Spring". In the daily operation, I believe that many people have doubts about the asynchronous threading method of Spring. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "what is the asynchronous threading method of Spring?" Next, please follow the editor to study!

Spring encapsulates the thread pool and thread calls of JDK, and multi-thread calls can be opened using tags.

First, configure a thread pool for Spring.

@ Configuration@EnableAsyncpublic class ThreadPoolConfig implements AsyncConfigurer {@ Bean @ Override public Executor getAsyncExecutor () {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor (); executor.setCorePoolSize (Runtime.getRuntime (). AvailableProcessors ()); executor.setMaxPoolSize (Runtime.getRuntime (). AvailableProcessors () * 5); executor.setQueueCapacity (Runtime.getRuntime (). AvailableProcessors () * 2); executor.setThreadNamePrefix ("this-executor-"); executor.initialize (); return executor @ Bean @ Override public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler () {return new SimpleAsyncUncaughtExceptionHandler ();}}

Then write a Service asynchronous method call, here is a return value, the method without a return value is relatively simple, there is no example here.

@ Service@Slf4jpublic class ThreadTasks {/ * if an asynchronous method has a return value, be sure to use a Future wrapper, otherwise you cannot return * @ return * @ throws InterruptedException * / @ Async public Future startTask () throws InterruptedException {Thread.sleep (3000); log.info ("this is async task"); return new AsyncResult ("123456");}}

Finally, write a Controller to call the asynchronous method.

RestControllerpublic class AsyncTaskController {@ Autowired private ThreadTasks tasks; @ GetMapping ("/ users-anon/useTask") public String useSyncTask () throws InterruptedException, ExecutionException {Future future = tasks.startTask (); return future.get ();}}

Finally, we can see that each time it is executed by a different thread.

2020-09-21 16 this-executor-1 33 INFO 53.027 INFO [user-center,3e635fca3d1259da,d30e8b6372922137,false] 1126-[this-executor-1] com.cloud.user.service.ThreadTasks: this is async task2020-09-21 16 purge 35 27.664 INFO [user-center,9b68efbb2c848d7b,895ed921f6c8fe03,false] 1126-[this-executor-2] com.cloud.user.service.ThreadTasks: this is async task so far The study on "what is the asynchronous threading method of Spring" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report