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

成都网站工作室用php做的网站必备那些文件

成都网站工作室,用php做的网站必备那些文件,做网站的专业词汇,wordpress 新建导航说明#xff1a;在一些场景#xff0c;如导入数据#xff0c;批量插入数据库#xff0c;使用常规方法#xff0c;需要等待较长时间#xff0c;而使用线程池可以提高效率。本文介绍如何在Spring Boot中使用线程池来批量插入数据。 搭建环境 首先#xff0c;创建一个Spr…说明在一些场景如导入数据批量插入数据库使用常规方法需要等待较长时间而使用线程池可以提高效率。本文介绍如何在Spring Boot中使用线程池来批量插入数据。 搭建环境 首先创建一个Spring Boot项目pom文件如下 ?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.7.12/versionrelativePath//parentgroupIdcom.hezy/groupIdartifactIdthread_pool_demo/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source11/maven.compiler.sourcemaven.compiler.target11/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependencydependencygroupIdcom.alibaba/groupIdartifactIddruid-spring-boot-starter/artifactIdversion1.2.8/version/dependencydependencygroupIdcom.mysql/groupIdartifactIdmysql-connector-j/artifactIdscoperuntime/scope/dependencydependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion2.2.2/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdcn.hutool/groupIdartifactIdhutool-all/artifactIdversion5.8.6/version/dependency/dependencies /project写一个插入数据的Mapper方法 import com.hezy.pojo.User; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param;Mapper public interface UserMapper {Insert(insert into i_users (username, password) values (#{user.username}, #{user.password}))void insert(Param(user) User user); }写一个接口用来插入20万条记录如下 import com.hezy.pojo.User; import com.hezy.service.AsyncService; import com.hezy.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;RestController RequestMapping(user) public class UserController {/*** 总记录数*/private final static int SIZE 40 * 10000;Autowiredprivate UserService userService;Autowiredprivate AsyncService asyncService;GetMapping(insert1)public void insert1() {ArrayListUser list new ArrayList(SIZE);for (int i 0; i SIZE; i) {User user new User();user.setUsername(user i);user.setPassword(password i);list.add(user);}long startTime System.currentTimeMillis();// 批量插入for (User user : list) {userService.insert(user);}long endTime System.currentTimeMillis();System.out.println(不用线程池插入40万条记录耗时: ((endTime - startTime) / 1000) s);} }启动项目测试一下看要多长时间……11分钟 使用线程池 Spring Boot有自动注入的线程池threadPoolTaskExecutor可以手动设置一些属性为我们所用。 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; import java.util.concurrent.ThreadPoolExecutor;Configuration EnableAsync public class ThreadPoolConfig {Bean(name threadPoolTaskExecutor)public Executor threadPoolTaskExecutor() {ThreadPoolTaskExecutor executor new ThreadPoolTaskExecutor();executor.setCorePoolSize(20);executor.setMaxPoolSize(40);executor.setQueueCapacity(500);executor.setKeepAliveSeconds(60);executor.setThreadNamePrefix(hezy-);executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());executor.initialize();return executor;} }使用线程池来完成上面插入数据的操作如下 GetMapping(insert2)public void insert2() {ArrayListUser list new ArrayList(SIZE);for (int i 0; i SIZE; i) {User user new User();user.setUsername(user i);user.setPassword(password i);list.add(user);}// 将数据分成4000批每批插入100条ListListUser batchList new ArrayList();for (int i 0; i list.size(); i 100) {batchList.add(list.subList(i, i 100));}long startTime System.currentTimeMillis();CountDownLatch countDownLatch new CountDownLatch(batchList.size());// 线程池分批插入for (ListUser batch : batchList) {asyncService.executeAsync(batch, userService, countDownLatch);}try {countDownLatch.await();} catch (InterruptedException e) {throw new RuntimeException(e);}long endTime System.currentTimeMillis();System.out.println(使用线程池插入40万条记录耗时: ((endTime - startTime) / 1000) s);}AsyncService实现类 import com.hezy.pojo.User; import com.hezy.service.AsyncService; import com.hezy.service.UserService; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service;import java.util.List; import java.util.concurrent.CountDownLatch;Service public class AsyncServiceImpl implements AsyncService {Async(threadPoolTaskExecutor)Overridepublic void executeAsync(ListUser batch, UserService userService, CountDownLatch countDownLatch) {try {for (User user : batch) {userService.insert(user);}} finally {countDownLatch.countDown();}} }启动测试速度提升很明显。如果再改造一下insert()方法一次插入多条数据肯定还能更快。 总结 本文介绍如何使用Spring Boot装配的线程池Bean完成大数据量的批量插入操作提高程序执行效率。 实例完整代码https://github.com/HeZhongYing/thread_pool_demo 参考B站UP主孟哥说Java视频https://www.bilibili.com/video/BV18r421F7CQ
http://www.dnsts.com.cn/news/26223.html

相关文章:

  • 临沂网站建设推广建设网站托管费用
  • 济南中建设计院网站视频网站怎么做统计
  • 主色调为绿色的网站江苏中盛建设集团网站
  • pageadmin wordpress石家庄百度提升优化
  • 网站开发技术难度深圳网站建设 龙华信科
  • 网站建设好吗湖南装修公司排名10名
  • 昆明网站建设优化网站建设未完成
  • ps如何做切片网站按钮网站建设分为哪几种类型
  • 企业网站备案需要法人拍照吗网站建设网站需求分析报告功能
  • 陕西网站建设陕icp备网站建设方案范本
  • 网站标题做参数有关wordpress教学的网站
  • 网站开发软件开发流程图湘潭网站建设 x磐石网络
  • 手机网站seo怎么做作图网站
  • 做自己的网站要多少钱dw做的网站如何上传图片
  • 苏州网站快速推广创建一个网站需要做哪些工作
  • 制作英文网站泰州网站建设专业团队
  • 安徽芜湖网站建设长沙企业seo优化
  • 做网商哪个国外网站好ftp上传网站全教程
  • 国外公共空间设计网站注册网站做推广
  • 潍坊网站制作保定公司电话网页模板免费资源
  • 晚上奖励自己的网站推荐wordpress app中文版下载
  • 哪里可以建网站天津塘沽爆炸
  • 易网拓营销型网站西峡网站开发
  • 快速学制作网站珠海做网站哪里公司好
  • 网站开发使用的语言类网站开发的课程
  • 原创作文网站怎样编辑网站标题
  • wordpress里验证谷歌站长电子商务网站平台建设策划
  • 教学网站在线自测功能怎么做北京做手机网站设计
  • 专业商城网站搭建价格怎么做页码
  • 中国建站网c2c模式的特点有哪些