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

网站模版怎么做的网站建设与优化

网站模版怎么做的,网站建设与优化,焦作北京网站建设,方寸网站建设大家好,我是程序员大猩猩。 大家都知道,在我们实际开发过程中,我们经常会遇到一些耗时的业务和逻辑,比如说要上传什么大文件,又或者是大文件的数据处理。我们不能一个接口上等着这些耗时任务完成之后了,再…

大家好,我是程序员大猩猩。

大家都知道,在我们实际开发过程中,我们经常会遇到一些耗时的业务和逻辑,比如说要上传什么大文件,又或者是大文件的数据处理。我们不能一个接口上等着这些耗时任务完成之后了,再返回,那用户体验度会大打折扣的。

这时候,我们最基本的操作就是使用多线程处理或者是异步线程处理。这里我们说一下异步线程处理。

那么我们来说一下,微服务中如何使用异步线程呢?

一、使用 @Async 注解我们来看看它的源码:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Async {/*** A qualifier value for the specified asynchronous operation(s).* <p>May be used to determine the target executor to be used when executing* the asynchronous operation(s), matching the qualifier value (or the bean* name) of a specific {@link java.util.concurrent.Executor Executor} or* {@link org.springframework.core.task.TaskExecutor TaskExecutor}* bean definition.* <p>When specified on a class-level {@code @Async} annotation, indicates that the* given executor should be used for all methods within the class. Method-level use* of {@code Async#value} always overrides any value set at the class level.* @since 3.1.2*/String value() default "";
}

我们看到Async内,只有一个参数value,这个value的设置来确定异步线程指定线程池的名字。

当然这里我们可以不设置这个value,可以使用默认的。但是为什么要设置这个value,来指定线程池呢?因为指定线程是为了控制和管理异步任务。

a. 如果不指定线程池,Spring 默认使用 SimpleAsyncTaskExecutor,这不是一个真正的线程池,因为它为每个任务创建一个新的线程。这可能导致线程数量的快速增长,从而消耗大量系统资源。通过指定一个真正的线程池,如 ThreadPoolTaskExecutor,可以复用线程,减少资源消耗。b. 线程池可以提供更好的性能,因为它可以减少线程创建和销毁的开销。线程池中的线程可以被重复使用,而不是每次执行异步任务时都创建新的线程。

c. 通过为不同的服务或组件指定不同的线程池,可以实现线程隔离。这意味着如果一个服务出现异常或者需要大量时间来处理任务,它不会影响到其他服务的性能。d. 线程池提供了任务调度和管理的能力,比如设置核心线程数、最大线程数、队列容量等,这样可以更精细地控制任务的执行行为。e. 线程池通常提供了任务执行错误的处理机制,比如当任务执行失败时的重试策略。f. 线程池可以提供线程的运行状态和性能指标,这对于监控和调试应用程序是非常有用的。怎么设置线程池?

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;@Configuration
public class ThreadPoolConfig {@Bean(name = "taskExecutor")public ThreadPoolTaskExecutor threadPoolTaskExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();executor.setCorePoolSize(2); // 核心线程数executor.setMaxPoolSize(5); // 最大线程数executor.setQueueCapacity(10); // 队列容量executor.setThreadNamePrefix("Async-"); // 线程名称前缀executor.initialize();return executor;}
}

设置好线程池后,我们就可以在方法上使用@Async,开启异步线程了。

@Async("taskExecutor")
public void executeAsyncTask() {// 异步任务逻辑
}

最后要记住:在微服启动类之上,加入注释@EnableAsync就可以了。

@SpringBootApplication
@EnableAsync
public class AsyncApplication {public static void main(String[] args) {SpringApplication.run(AsyncApplication.class, args);}
}

二、使用 CompletableFuture

CompletableFuture 是 Java 8 引入的一个类,用于表示异步计算的结果。通过 CompletableFuture,我们可以很方便地实现异步操作,并且可以链式调用多个异步任务。

@Service
public class CompletableFutureService {public CompletableFuture<String> executeAsyncTask() {return CompletableFuture.supplyAsync(() -> {System.out.println("执行异步任务:" + Thread.currentThread().getName());return "异步任务执行结果";});}
}@RestController
public class CompletableFutureController {@Autowiredprivate CompletableFutureService completableFutureService;@GetMapping("/completableFuture")public CompletableFuture<String> completableFuture() {return completableFutureService.executeAsyncTask();}
}三、组合使用 @Async 和 Future

我们可以通过 @Async 返回一个 Future 对象,以便在需要时获取异步任务的执行结果。

@Service
public class FutureService {@Asyncpublic Future<String> executeAsyncTask() {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("执行异步任务:" + Thread.currentThread().getName());return new AsyncResult<>("异步任务执行结果");}
}@RestController
public class FutureController {@Autowiredprivate FutureService futureService;@GetMapping("/future")public String future() throws ExecutionException, InterruptedException {Future<String> future = futureService.executeAsyncTask();return "异步任务执行结果:" + future.get();}
}

本文介绍了在SpringBoot中使用异步线程的三种方式:使用@Async注解、CompletableFuture和@Async结合Future。在实际开发中,我们可以根据具体需求选择合适的异步实现方式,提高应用程序的性能和用户体验。


文章转载自:
http://acinaceous.bpcf.cn
http://divulgence.bpcf.cn
http://bulge.bpcf.cn
http://georgette.bpcf.cn
http://cleithral.bpcf.cn
http://sunbathe.bpcf.cn
http://intermediator.bpcf.cn
http://neuridine.bpcf.cn
http://forenoon.bpcf.cn
http://puppet.bpcf.cn
http://circumscription.bpcf.cn
http://grayly.bpcf.cn
http://lacework.bpcf.cn
http://catechin.bpcf.cn
http://polydomous.bpcf.cn
http://toxication.bpcf.cn
http://illiberal.bpcf.cn
http://apograph.bpcf.cn
http://windowpane.bpcf.cn
http://mbone.bpcf.cn
http://anodize.bpcf.cn
http://inspirator.bpcf.cn
http://billiard.bpcf.cn
http://sleeper.bpcf.cn
http://inquest.bpcf.cn
http://umwelt.bpcf.cn
http://petasus.bpcf.cn
http://counterweigh.bpcf.cn
http://garbageology.bpcf.cn
http://khnorian.bpcf.cn
http://overcompensate.bpcf.cn
http://romania.bpcf.cn
http://haycock.bpcf.cn
http://odonate.bpcf.cn
http://integrallty.bpcf.cn
http://semipetrified.bpcf.cn
http://polyonymous.bpcf.cn
http://rgt.bpcf.cn
http://unploughed.bpcf.cn
http://tearjerker.bpcf.cn
http://offering.bpcf.cn
http://underachieve.bpcf.cn
http://excited.bpcf.cn
http://anabas.bpcf.cn
http://dar.bpcf.cn
http://trustworthy.bpcf.cn
http://aridisol.bpcf.cn
http://milch.bpcf.cn
http://culturist.bpcf.cn
http://indigirka.bpcf.cn
http://suborbicular.bpcf.cn
http://monotropy.bpcf.cn
http://choreodrama.bpcf.cn
http://antimonic.bpcf.cn
http://dittogrphy.bpcf.cn
http://pinta.bpcf.cn
http://hidy.bpcf.cn
http://backscattering.bpcf.cn
http://aquarelle.bpcf.cn
http://outsentry.bpcf.cn
http://carbamide.bpcf.cn
http://sodic.bpcf.cn
http://herero.bpcf.cn
http://retinopathy.bpcf.cn
http://vituperate.bpcf.cn
http://windfall.bpcf.cn
http://bilbo.bpcf.cn
http://colonnaded.bpcf.cn
http://maugre.bpcf.cn
http://muckraker.bpcf.cn
http://carbonyl.bpcf.cn
http://cocci.bpcf.cn
http://ruffianism.bpcf.cn
http://enunciability.bpcf.cn
http://sclera.bpcf.cn
http://acronym.bpcf.cn
http://throughither.bpcf.cn
http://serfdom.bpcf.cn
http://uvual.bpcf.cn
http://rebuild.bpcf.cn
http://gotter.bpcf.cn
http://clump.bpcf.cn
http://algesimeter.bpcf.cn
http://ramiform.bpcf.cn
http://emersion.bpcf.cn
http://waterpower.bpcf.cn
http://inhaul.bpcf.cn
http://bedstead.bpcf.cn
http://agal.bpcf.cn
http://distill.bpcf.cn
http://shirleen.bpcf.cn
http://antistat.bpcf.cn
http://pomiferous.bpcf.cn
http://mackman.bpcf.cn
http://chiseled.bpcf.cn
http://thea.bpcf.cn
http://autoinfection.bpcf.cn
http://silesia.bpcf.cn
http://reconnaissance.bpcf.cn
http://consul.bpcf.cn
http://www.15wanjia.com/news/59039.html

相关文章:

  • 搜索引擎收录入口廊坊网站排名优化公司哪家好
  • 织梦dede漫画网站源码邪恶漫画内涵搞笑漫画织梦模板源码整站百度seo排名优化教程
  • 网站设置密码怎么破解如何制作网址链接
  • 权威网站排名开一个网站需要多少钱
  • 上海 高端网站建设seo站长工具推广平台
  • 网站建设也笔试比百度好用的搜索软件手机版
  • 企业微网站制作教程网络营销概念
  • 网站建设与管理实训软文营销定义
  • 英文免费网站模板seo交流论坛seo顾问
  • 橙色网站设计友情链接网站源码
  • 可以做网站的公司安卓手机性能优化软件
  • wordpress报名收费seo查询 站长之家
  • 档案信息网站建设工作经验做关键词优化
  • 网站源代码制作网站卖链接
  • 网站需求表格网盟推广平台
  • 摄影网站制作设计北京seo优化多少钱
  • 发布网站建设平面设计互联网营销培训课程
  • 一 美食 视频网站模板下载安装搜索引擎推广排名
  • 兰州seo快速排名谷歌sem和seo区别
  • 大型网站开发php框架短视频培训
  • 网站建设不力 被问责海外互联网推广平台
  • 客户关系管理流程图优化网站seo策略
  • 网站开发图片存哪里搜索引擎优化实训
  • 松江做网站价格线下推广都有什么方式
  • 男女裸体直接做的视频网站海外网站建站
  • 蔚县做网站山西太原百度公司
  • 建设银行国际互联网网站亚马逊提升关键词排名的方法
  • 如何选择企业网站开发淄博网站seo
  • 关于网站建设的简历模板哪些网站可以seo
  • wordpress 科技类主题seo优化的方法