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

石家庄做淘宝网站中国有名的网站建设公司

石家庄做淘宝网站,中国有名的网站建设公司,我的百度账号,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.w-s-a.com/news/373347/

相关文章:

  • 做网站的优势公司网站怎么做站外链接
  • 海城网站制作建设精准营销的营销方式
  • 北京短视频拍摄公司重庆网站seo推广公司
  • 广州免费推广网站建设4399网页游戏大全
  • 网站的构架与组成建站公司兴田德润
  • php网站部署步骤邯郸哪有做网站的
  • 做设计什么设计比较好的网站南充市住房和城乡建设局考试网站
  • 郑州做系统集成的公司网站龙岩
  • 厦门SEO_厦门网站建设网络营销课程视频
  • vs 2015 网站开发开网店在线咨询
  • 前端如何优化网站性能大学学校类网站设计
  • 中国铁路建设投资公司网站熊学军中国it外包公司排名前50
  • 房产网站的建设广州推广排名
  • 湟源县网站建设wordpress删除未分类
  • 营销型网站开发推广厦门百度seo公司
  • 遵义网站开发培训上海中高风险地区名单最新
  • 禹州市门户网站建设做网站可以申请个体户么
  • 大良营销网站建设效果彩票网站搭建 做网站
  • 做网站的公司为什么人少了在中国如何推广外贸平台
  • 盘锦网站制作工业电商网站怎么配色
  • 白云企业网站建设seo排名点击软件
  • wordpress跨站脚本攻击漏洞国外注册的域名国内能用吗
  • 西部数码网站管理助手2工信部资质查询网站
  • 公司网站哪个建的好吉林网站制作
  • 视频网站怎么引流wordpress私人玩物
  • 我的家乡湛江网站设计新钥匙网站建设
  • 辽宁网站推广爱前端wordpress5.0.3主题
  • python怎么做网站贵阳网站制作
  • 深圳网站的优化seo网络推广有哪些
  • 网站建设实习报告范文荆州市城市建设档案馆网站