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

做女装的网站百度推广一年要多少钱

做女装的网站,百度推广一年要多少钱,巨量千川推广怎么收费,建筑模板915 1830价格文章目录 一、简介二、异步任务Async的使用方法2.1、第一步、配置类上加EnableAsync注解2.2、第二步、自定义线程池2.2.1、方法一、不配置自定义线程池使用默认线程池2.2.2、方法二、使用AsyncConfigurer指定线程池2.2.3、方法三、使用自定义的线程池Excutor2.2.4、方法四、使用…

文章目录

  • 一、简介
  • 二、异步任务Async的使用方法
    • 2.1、第一步、配置类上加@EnableAsync注解
    • 2.2、第二步、自定义线程池
      • 2.2.1、方法一、不配置自定义线程池使用默认线程池
      • 2.2.2、方法二、使用AsyncConfigurer指定线程池
      • 2.2.3、方法三、使用自定义的线程池Excutor
      • 2.2.4、方法四、使用动态线程池来创建
    • 2.3、第三步、在需要异步处理的方法上加@Async注解
  • 三、源码解析
  • 四、总结

一、简介

最近工作中接触到了 Spring 的 @Async 注解,有了了解其使用方法和源码的想法,所以有了这篇文章,本文源码来自Spring6.1.10

二、异步任务Async的使用方法

2.1、第一步、配置类上加@EnableAsync注解

在任意配置类上增加 @EnableAsync 注解,表示启用异步任务

@Configuration
@EnableAsync
public class MyConfig {
}

也可以加 SpringBoot 启动类上,因为 @SpringBootApplication 注解由 @Configuration 组成

2.2、第二步、自定义线程池

2.2.1、方法一、不配置自定义线程池使用默认线程池

如果不配置自定义的线程池,Spring会默认获取 TaskExecutor 类型的线程池,再获取不到,会获取名为 taskExecutorExecutor 类型的线程池,其实是由 TaskExecutionAutoConfiguration 自动注入的,可以通过 spring.task.execution.xxx 来更改其配置

2.2.2、方法二、使用AsyncConfigurer指定线程池

写一个类实现 AsyncConfigurer 接口,实现 getAsyncExecutorgetAsyncUncaughtExceptionHandler 方法,注意这个类要给 Spring 托管,所以要加上 @Component 注解

@Component
public class MyAsyncConfigurer implements AsyncConfigurer {@Overridepublic Executor getAsyncExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();//核心线程数executor.setCorePoolSize(5); //最大线程数executor.setMaxPoolSize(10); //队列容量executor.setQueueCapacity(200); //允许线程空闲时间(秒)executor.setKeepAliveSeconds(10);//线程名称前缀executor.setThreadNamePrefix("custom-"); executor.initialize();return executor;}@Overridepublic AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {//异步任务未被捕获时的处理return new SimpleAsyncUncaughtExceptionHandler();}
}

2.2.3、方法三、使用自定义的线程池Excutor

不论是方法一还是方法二都有一个弊端,那就是所有的异步任务都会使用同一个线程池,所以可以使用方法三来定义多个线程池,通过实例 Bean 的方式把 Excutor 注入 Spring,并指定 Bean 的名称

@Configuration
public class CustomThreadPoolConfig {@Bean(name = "customExecutor")public ThreadPoolTaskExecutor customExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();//核心线程数executor.setCorePoolSize(5); //最大线程数executor.setMaxPoolSize(10); //队列容量executor.setQueueCapacity(200); //允许线程空闲时间(秒)executor.setKeepAliveSeconds(10);//线程名称前缀executor.setThreadNamePrefix("custom-"); executor.initialize();return executor;}
}

2.2.4、方法四、使用动态线程池来创建

使用 dynamic-tp 动态线程池配置,这里就不展开了,有兴趣的可以去查阅资料,原理就是把 2.2.3 的 Bean 放到了配置文件里,并且可以动态改变参数

2.3、第三步、在需要异步处理的方法上加@Async注解

最后再需要异步处理的方法上增加 @Async 注解

@Service
public class MyServiceImpl implements MyService {@Asyncpublic void asyncMethod()  {log.info("test");}}

如果选用 2.2.3或者 2.2.4 的话,还需要在 @Async 上指定线程池的名称

@Service
public class MyServiceImpl implements MyService {@Async("customExecutor")public void asyncMethod()  {log.info("test");}}

三、源码解析

先从 @EnableAsync 注解开始

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AsyncConfigurationSelector.class)
public @interface EnableAsync {
}

可以看到通过 @Import 注解导入了 AsyncConfigurationSelector 类,这里不展开讲 @Import 注解了(想了解@Import注解的可以看我的另一篇文章:@Import注解源码解析),只需要知道这个注解导入的类 AsyncConfigurationSelectorString[] selectImports(AnnotationMetadata importingClassMetadata); 方法会在容器启动时执行,这个方法在其抽象父类 AdviceModeImportSelector 里,我们看下这个方法

image-20240724161425849

这里其实就是拿到 @EnableAsync 注解的 AdviceMode,再调用子类的 selectImports 方法,而 @EnableAsync 注解的 AdviceMode 的默认值是 AdviceMode.PROXY,再来看子类 AsyncConfigurationSelectorselectImports(AdviceMode adviceMode) 方法

image-20240724161955513

因为是 AdviceMode.PROXY,所以走的红框中的代码,我们继续看这个 ProxyAsyncConfiguration

image-20240724162908330

这个类里注册了一个 AsyncAnnotationBeanPostProcessor 类,并且调用了 configure 方法把 executorexceptionHandler 传入,这个executorexceptionHandler 是哪来的呢,在它的抽象父类 AbstractAsyncConfiguration 里赋的值,我们看下 AbstractAsyncConfigurationsetConfigurers 方法

image-20240724170943106

可以看到,就是我们之前 2.2.2 中用到的AsyncConfigurer,只要我们定义了实现了 AsyncConfigurer 接口的Bean,这里就把它的两个方法作为函数式接口赋值到 executorexceptionHandler 里,后面会用上

现在我们再回头看下 AsyncAnnotationBeanPostProcessor 的类图

image-20240724163645599

他是一个继承了 AbstractAdvisingBeanPostProcessor 抽象类的 BeanPostProcessor(想了解BeanPostProcessor的可以看我的另一篇文章:Spring后置处理器BeanFactoryPostProcessor与BeanPostProcessor源码解析),这个 AbstractAdvisingBeanPostProcessor 其实是 Spring AOP体系结构中非常重要的一个类,当我们想法实现一个切面的时候,可以扩展这个类,实现自己的Advisor,就可以在 postProcessAfterInitialization 方法里根据需要创建代理类,这里我们看看 AsyncAnnotationBeanPostProcessor 是如何实现这个 Advisor 的,可以在 AsyncAnnotationBeanPostProcessorsetBeanFactory 方法里找到,如下:

image-20240724170459036

这个创建了一个 AsyncAnnotationAdvisor,并把上文提到的 executorexceptionHandler 两个函数式接口传入 ,我们看下 AsyncAnnotationAdvisor 的这个构造函数

image-20240724171449116

可以看到构建了 advice 和 pointcut,这两个可以简单理解为 advice 定义了要执行的代码,而pointcut 定义了在哪里执行这些代码,这个 pointcut 很简单,我们可以到传进去的 Annotation 集合就是 Async,表示带 @Async 注解的就是切点,下面重点看下 advice,跟进下 buildAdvice 方法

image-20240724173421318

这里创建了 AnnotationAsyncExecutionInterceptor 并调用了 configure 方法,我们先看下 AnnotationAsyncExecutionInterceptor 的类图

image-20240724174103882

可以看到 AnnotationAsyncExecutionInterceptor 是实现了 MethodInterceptor 接口的,所以在调用被代理方法前,会先调用其 invoke 方法,我们在其父类 AsyncExecutionInterceptor 里找到这个 invoke 方法

image-20240724174730274

可以看到先获取 Executor,然后创线程任务,任务中调用了被代理的方法,最后把任务提交到线程池中,所以加上 @Async 注解的方法会在线程池中异步执行,下面我们重点看看这个 Executor 是怎么获取的,跟进 determineAsyncExecutor 方法

image-20240724175753253

可以看到,如果 @Async 后配置了线程池的名字,会从bean工厂里找对应的 Executor 返回,否则返回默认的 Executor,我们再来看默认的 Executor 是什么,回头看 AnnotationAsyncExecutionInterceptorconfigure 方法,在其父类 AsyncExecutionAspectSupport

image-20240724180050822

传进来的 defaultExecutorexceptionHandler 就是我们之前提到的 AsyncConfigurer 实现类的两个函数式接口,再贴个图,防止大家忘了

image-20240724170943106

defaultExecutor 如果没有,会调用 getDefaultExecutor 方法,exceptionHandler 如果没有,会默认使用 SimpleAsyncUncaughtExceptionHandler ,我们看下 getDefaultExecutor 方法

image-20240724180555577

先获取 TaskExecutor 类型的线程池,如果获取不到,会获取名为 taskExecutorExecutor 类型的线程池(DEFAULT_TASK_EXECUTOR_BEAN_NAME = “taskExecutor”)

四、总结

其实 @Async 注解就是利用 Spring AOP 给类加了代理,当需要执行带 @Async 的方法时,会将其包装成 task 提交到线程池中异步执行,如果在 @Async 注解上定义线程池的名字,会用对应的线程池执行,否则使用 AsyncConfigurer 实现类中的 getAsyncExecutor 方法返回的 Executor 执行,如果未配置 AsyncConfigurer 实现类,则使用 TaskExecutionAutoConfiguration 配置类创建的 Executor 执行


文章转载自:
http://zincate.spbp.cn
http://gondolet.spbp.cn
http://auld.spbp.cn
http://oahu.spbp.cn
http://haemostasis.spbp.cn
http://iodometry.spbp.cn
http://generalship.spbp.cn
http://frazil.spbp.cn
http://triethylamine.spbp.cn
http://monitor.spbp.cn
http://lawyer.spbp.cn
http://berkeleian.spbp.cn
http://celtic.spbp.cn
http://sassanian.spbp.cn
http://enterozoa.spbp.cn
http://tularemia.spbp.cn
http://hendecahedral.spbp.cn
http://bullock.spbp.cn
http://alkine.spbp.cn
http://knockabout.spbp.cn
http://spermatogenesis.spbp.cn
http://reprehensible.spbp.cn
http://symphonette.spbp.cn
http://countship.spbp.cn
http://amidogroup.spbp.cn
http://colorable.spbp.cn
http://swagged.spbp.cn
http://stolidly.spbp.cn
http://amidogen.spbp.cn
http://foldboat.spbp.cn
http://amphimictical.spbp.cn
http://underspin.spbp.cn
http://overoccupied.spbp.cn
http://inscription.spbp.cn
http://exhaust.spbp.cn
http://abolisher.spbp.cn
http://bluebutton.spbp.cn
http://workboard.spbp.cn
http://invention.spbp.cn
http://embed.spbp.cn
http://priscan.spbp.cn
http://dennet.spbp.cn
http://erbium.spbp.cn
http://laurie.spbp.cn
http://hackensack.spbp.cn
http://blackland.spbp.cn
http://saccharomycete.spbp.cn
http://lade.spbp.cn
http://sitology.spbp.cn
http://cyclonet.spbp.cn
http://hallah.spbp.cn
http://playclothes.spbp.cn
http://tableland.spbp.cn
http://spiniferous.spbp.cn
http://knuckleball.spbp.cn
http://firelock.spbp.cn
http://unpronounceable.spbp.cn
http://jaup.spbp.cn
http://buccaneer.spbp.cn
http://oratorian.spbp.cn
http://newfangle.spbp.cn
http://request.spbp.cn
http://buffo.spbp.cn
http://anemometry.spbp.cn
http://megalops.spbp.cn
http://unversed.spbp.cn
http://amontillado.spbp.cn
http://trimonthly.spbp.cn
http://bund.spbp.cn
http://melchior.spbp.cn
http://chatelet.spbp.cn
http://immy.spbp.cn
http://zirconium.spbp.cn
http://supereminence.spbp.cn
http://strutter.spbp.cn
http://glasshouse.spbp.cn
http://crozier.spbp.cn
http://commingle.spbp.cn
http://ferryman.spbp.cn
http://procaryote.spbp.cn
http://fledgeless.spbp.cn
http://eleaticism.spbp.cn
http://catchall.spbp.cn
http://rhododendra.spbp.cn
http://calcinator.spbp.cn
http://buttercup.spbp.cn
http://voluptuary.spbp.cn
http://lankily.spbp.cn
http://biauriculate.spbp.cn
http://wiretap.spbp.cn
http://forename.spbp.cn
http://undervalue.spbp.cn
http://cardoon.spbp.cn
http://hypoxemia.spbp.cn
http://fleshings.spbp.cn
http://excarnation.spbp.cn
http://quay.spbp.cn
http://neurochemical.spbp.cn
http://hemlock.spbp.cn
http://circumjovial.spbp.cn
http://www.hrbkazy.com/news/87534.html

相关文章:

  • 自己接私单网站开发关键词排名推广怎么做
  • b站推广网站2024mmm不用下载seo基础视频教程
  • 自己做网站下载怎么网站seo课程
  • 郑州网站建设招商百度收录技术
  • 哪些网站可以做驾考试题平台网站开发公司
  • 做网站和微信公众号需要多少钱做网站需要什么条件
  • 美食网站建设策划书黄冈seo
  • app软件开发学什么专业杭州网络推广网络优化
  • 中国移动官方网站优质网站
  • 做网站的eclipb站推广入口在哪
  • 硅胶模具技术支持东莞网站建设seo算法培训
  • 网站程序预装软文范例100字
  • 灵犀科技 高端网站建设背景图友情链接是免费的吗
  • 工程公司logo图标设计优化大师怎么样
  • 杭州蚂蚁 做网站的公司做网络推广有前途吗
  • 网站建设品牌公司搜索百度网址网页
  • html做校园网站营销型网站建设专家
  • 百度站长平台清退搜索引擎营销案例分析题
  • 中国室内设计网站排名网络营销方法有哪些?
  • 广州中企动力网站制作百度客服人工电话24
  • 沈阳哪家做网站好seo优化网站排名
  • 甘肃庆阳西峰区疫情seo营销外包公司
  • 洋桥网站建设公司手机导航下载2022新版
  • 亚马逊网站托管怎么做网站上做推广
  • flash网站建设技术搜索词热度查询
  • 个人做百度云下载网站吗网站排名推广推荐
  • 做视频图片博客网站有哪些企业网络营销推广方案策划范文
  • 云虚拟主机可以做多少个网站网络推广的公司是骗局吗
  • 网站下方一般放什么对网络营销的认识有哪些
  • wordpress如何进入后台上海排名seo公司