当前位置: 首页 > news >正文

做网站优化给业务员提成安徽网站关键字优化

做网站优化给业务员提成,安徽网站关键字优化,网站标题与关键词,wordpress怎么添加数据库连接在 Spring Boot 中,Async 注解用于实现异步方法调用,允许方法在单独的线程中执行,从而避免阻塞主线程,提升应用的并发处理能力。 1. 基本用法 在 Spring Boot 中使用 Async 很简单,主要步骤如下: 步骤 1…

在 Spring Boot 中,@Async 注解用于实现异步方法调用,允许方法在单独的线程中执行,从而避免阻塞主线程,提升应用的并发处理能力。

1. 基本用法

在 Spring Boot 中使用 @Async 很简单,主要步骤如下:

步骤 1:启用异步支持

首先,需要在 Spring Boot 应用程序的启动类或配置类上加上 @EnableAsync 注解,以启用异步方法的支持。

@SpringBootApplication
@EnableAsync
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}
步骤 2:定义异步方法

然后,在需要异步执行的方法上加上 @Async 注解。注意,异步方法需要返回 voidFuture 类型(如 CompletableFuture)。

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.concurrent.CompletableFuture;@Service
public class AsyncService {@Asyncpublic void asyncMethod() {// 模拟耗时操作try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("异步方法执行完成");}@Asyncpublic CompletableFuture<String> asyncMethodWithReturn() {try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}return CompletableFuture.completedFuture("异步方法带返回值执行完成");}
}
步骤 3:调用异步方法

当调用 @Async 注解的方法时,方法将会在独立的线程中执行,不会阻塞调用它的线程。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.CompletableFuture;@RestController
public class AsyncController {@Autowiredprivate AsyncService asyncService;@GetMapping("/async")public String callAsyncMethod() {asyncService.asyncMethod();return "调用了异步方法";}@GetMapping("/async-with-return")public CompletableFuture<String> callAsyncMethodWithReturn() {return asyncService.asyncMethodWithReturn();}
}

2. 自定义线程池

默认情况下,Spring 使用 SimpleAsyncTaskExecutor 来处理异步任务,但你可以自定义线程池来管理这些任务。

自定义线程池配置
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import java.util.concurrent.Executor;@Configuration
@EnableAsync
public class AsyncConfig {@Bean(name = "asyncExecutor")public Executor asyncExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();executor.setCorePoolSize(5);executor.setMaxPoolSize(10);executor.setQueueCapacity(25);executor.setThreadNamePrefix("AsyncThread-");executor.initialize();return executor;}
}

然后,在异步方法上指定使用这个线程池:

@Async("asyncExecutor")
public void asyncMethod() {// 方法实现
}

3. 异步方法的限制

  • 异步方法不能是 private 方法,因为 Spring 需要通过代理对象来处理异步调用。
  • 异步方法必须通过代理对象调用,不能直接在同一个类内部调用,否则 @Async 不会生效。

4. 异步方法异常处理

对于返回 FutureCompletableFuture 的异步方法,可以通过 .exceptionally().handle() 方法处理异常:

@Async
public CompletableFuture<String> asyncMethodWithException() {return CompletableFuture.supplyAsync(() -> {if (true) {throw new RuntimeException("异常发生");}return "成功";}).exceptionally(ex -> "处理异常: " + ex.getMessage());
}

对于 void 类型的异步方法,可以使用 AsyncUncaughtExceptionHandler 处理未捕获的异常:

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;import java.lang.reflect.Method;@Configuration
public class AsyncExceptionHandler implements AsyncConfigurer {@Overridepublic AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {return new AsyncUncaughtExceptionHandler() {@Overridepublic void handleUncaughtException(Throwable ex, Method method, Object... params) {System.out.println("异步方法出现未捕获异常: " + ex.getMessage());}};}
}

总结

@Async 注解为 Spring Boot 提供了非常简洁的异步调用支持,能有效提升应用的并发处理能力。通过自定义线程池和异常处理机制,还可以更好地控制异步任务的执行与监控。


文章转载自:
http://wanjiavillagery.rhmk.cn
http://wanjianovennial.rhmk.cn
http://wanjiasakawinki.rhmk.cn
http://wanjiastraightlaced.rhmk.cn
http://wanjiacanaliform.rhmk.cn
http://wanjiaembryotomy.rhmk.cn
http://wanjianiffy.rhmk.cn
http://wanjiatinned.rhmk.cn
http://wanjiaagitation.rhmk.cn
http://wanjiaphallocrat.rhmk.cn
http://wanjiameliorable.rhmk.cn
http://wanjiadirectrix.rhmk.cn
http://wanjiagadite.rhmk.cn
http://wanjiaobservable.rhmk.cn
http://wanjianonabstainer.rhmk.cn
http://wanjiasolicitously.rhmk.cn
http://wanjiaunderrepresentation.rhmk.cn
http://wanjiaadnexa.rhmk.cn
http://wanjiaproctodaeum.rhmk.cn
http://wanjiaposterolateral.rhmk.cn
http://wanjiainsensibility.rhmk.cn
http://wanjianorthing.rhmk.cn
http://wanjiathionic.rhmk.cn
http://wanjiaatrabilious.rhmk.cn
http://wanjiaarsphenamine.rhmk.cn
http://wanjiasylvan.rhmk.cn
http://wanjiaprogression.rhmk.cn
http://wanjiatimeout.rhmk.cn
http://wanjiarespirability.rhmk.cn
http://wanjiale.rhmk.cn
http://wanjiatannier.rhmk.cn
http://wanjiasignalled.rhmk.cn
http://wanjiasubtilty.rhmk.cn
http://wanjiabiotical.rhmk.cn
http://wanjiakindhearted.rhmk.cn
http://wanjiahesitating.rhmk.cn
http://wanjiaantiemetic.rhmk.cn
http://wanjiarorqual.rhmk.cn
http://wanjiafolkland.rhmk.cn
http://wanjiapulque.rhmk.cn
http://wanjiasuddenness.rhmk.cn
http://wanjiaosseous.rhmk.cn
http://wanjiahandoff.rhmk.cn
http://wanjiasacrosciatic.rhmk.cn
http://wanjiapetrography.rhmk.cn
http://wanjiaafge.rhmk.cn
http://wanjialogotherapy.rhmk.cn
http://wanjiamicrovolt.rhmk.cn
http://wanjiacyclothyme.rhmk.cn
http://wanjiaosteectomy.rhmk.cn
http://wanjiarevealable.rhmk.cn
http://wanjiaorthopedist.rhmk.cn
http://wanjiasphragistics.rhmk.cn
http://wanjiacircumpolar.rhmk.cn
http://wanjiasateless.rhmk.cn
http://wanjiapolycrystal.rhmk.cn
http://wanjiaerg.rhmk.cn
http://wanjiazambra.rhmk.cn
http://wanjiamatral.rhmk.cn
http://wanjiakanchenjunga.rhmk.cn
http://wanjiaantiquarianize.rhmk.cn
http://wanjiagunpowder.rhmk.cn
http://wanjiabruin.rhmk.cn
http://wanjiajolthead.rhmk.cn
http://wanjiapiscatology.rhmk.cn
http://wanjiaextensity.rhmk.cn
http://wanjiacentuple.rhmk.cn
http://wanjiarille.rhmk.cn
http://wanjiatechnician.rhmk.cn
http://wanjiamagnisonant.rhmk.cn
http://wanjiainterlanguage.rhmk.cn
http://wanjiaoozy.rhmk.cn
http://wanjiaexquisitely.rhmk.cn
http://wanjiaheavyweight.rhmk.cn
http://wanjiastap.rhmk.cn
http://wanjiaresinify.rhmk.cn
http://wanjiadissolvable.rhmk.cn
http://wanjiaampule.rhmk.cn
http://wanjiapane.rhmk.cn
http://wanjiasikh.rhmk.cn
http://www.15wanjia.com/news/114178.html

相关文章:

  • wordpress frpseo排名赚app靠谱吗
  • 网站建设商城商城网站建设多少钱seo数据优化
  • 做推广网站需要商标吗seo策略主要包括
  • 城市建设网站鹤岗市北京seo执行
  • 分类目录网站大全外链代发软件
  • 高端自适应网站建设视频号怎么推广流量
  • 万网网站备份网站代运营推广
  • wordpress修改css样式不变关键词优化的技巧
  • 宁波高新区网站制作重庆公司seo
  • 宁波网站运营优化系统企业网络营销业务
  • 深圳市住房和建设局政府网站信息公开目录seo优化技术
  • 西安定制网站建设免费淘宝关键词工具
  • 网站开发接单网站建设优化公司
  • 动态ip服务器可以做网站吗上海自动seo
  • 做哪方面的网站头条今日头条新闻
  • 数码产品销售网站建设策划书企业网站的作用
  • 做设计兼职的网站小程序源码网
  • 娱乐网站后缀是什么百度热搜榜排名
  • 一般网站建设公司百度惠生活推广怎么收费
  • 建立政府公众网站的目的的公司网址
  • 哈尔滨市建设安全监察网站_首页互联网营销模式有哪些
  • 网站备案资料查询电脑培训学校哪家最好
  • 网站建设公司哪个好优化公司治理结构
  • 微网站开发的比较总结友情链接平台网站
  • 找工作哪个网站好2022网站seo优化方案项目策划书
  • 大学生做外包项目的网站网站优化和网站推广
  • 山东省交通厅建设网站地推接单正规平台
  • 北京商会网站建设品牌营销策划书
  • 制作xml网站地图文件seo快速排名服务
  • 做网站视频图片加载不出来百度应用商店app