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

CountDownLatch 批量更改使用,

代码

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.first.pet.platform.entity.PlatformAddress;
import com.first.pet.platform.mapper.PlatformAddressMapper;
import com.first.pet.platform.service.IPlatformAddressServiceTest;
import com.first.pet.threadPool.ThreadPoolUtils;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.CountDownLatch;/*** <p>* 服务类* </p>** @author yangquan* @since 2023-09-26*/
@Service
public class IPlatformAddressServiceTestImpl extends ServiceImpl<PlatformAddressMapper, PlatformAddress> implements IPlatformAddressServiceTest {@Resourceprivate PlatformAddressMapper platformAddressMapper;//每次查询500条数据操作/*** 组装数据*/@Override@Transactional(rollbackFor = Exception.class)public void assembleAddressData() throws InterruptedException {//        1.创建任务计数器  参数为设置任务数量//开三个线程,一个线程执行500条CountDownLatch countDownLatch = new CountDownLatch(4);//        2.开启三个线程  分别执行三个查询//        查询 性别信息ThreadPoolUtils.sqlThreadPool.submit(new Runnable() {@Overridepublic void run() {QueryWrapper<PlatformAddress> addressWrapper = new QueryWrapper<>();addressWrapper.last("limit 1500,500");List<PlatformAddress> platformAddresses = platformAddressMapper.selectList(addressWrapper);List<PlatformAddress> platformAddress = getPlatformAddress(platformAddresses);platformAddressMapper.updateDataById(platformAddress);//                得到查询结果//                计数器减一countDownLatch.countDown();}});//        查询 地区分布ThreadPoolUtils.sqlThreadPool.submit(new Runnable() {@Overridepublic void run() {QueryWrapper<PlatformAddress> addressWrapper = new QueryWrapper<>();addressWrapper.last("limit 2000,500");List<PlatformAddress> platformAddresses = platformAddressMapper.selectList(addressWrapper);List<PlatformAddress> platformAddress = getPlatformAddress(platformAddresses);platformAddressMapper.updateDataById(platformAddress);//                得到查询结果//                计数器减一countDownLatch.countDown();}});//        查询 注册量ThreadPoolUtils.sqlThreadPool.submit(new Runnable() {@Overridepublic void run() {QueryWrapper<PlatformAddress> addressWrapper = new QueryWrapper<>();addressWrapper.last("limit 2500,500");List<PlatformAddress> platformAddresses = platformAddressMapper.selectList(addressWrapper);List<PlatformAddress> platformAddress = getPlatformAddress(platformAddresses);platformAddressMapper.updateDataById(platformAddress);//                得到查询结果//                计数器减一countDownLatch.countDown();}});//        查询 注册量ThreadPoolUtils.sqlThreadPool.submit(new Runnable() {@Overridepublic void run() {QueryWrapper<PlatformAddress> addressWrapper = new QueryWrapper<>();addressWrapper.last("limit 3000,500");List<PlatformAddress> platformAddresses = platformAddressMapper.selectList(addressWrapper);List<PlatformAddress> platformAddress = getPlatformAddress(platformAddresses);platformAddressMapper.updateDataById(platformAddress);//                得到查询结果//                计数器减一countDownLatch.countDown();}});//        await() 当计数器为0的时候 主线程向下执行 没有这一步的话,如果一旦主线程向下执行// return map map中可能有的开启的线程还没有执行完毕,即返回的不是线程执行后的结果countDownLatch.await();}private List<PlatformAddress> getPlatformAddress(List<PlatformAddress> platformAddresses) {platformAddresses.stream().forEach(e -> {e.setInitials(ToFirstChar(e.getAddressName()).toUpperCase());e.setCompleteSpelling(ToPinyin(e.getAddressName()));});return platformAddresses;}public static void main(String[] args) {String ss = ToFirstChar("安徽省");System.out.println(ss);}/*** 获取字符串拼音的第一个字母** @param chinese* @return*/public static String ToFirstChar(String chinese) {String pinyinStr = "";
//		char[] newChar = chinese.toCharArray();  //转为单个字符char[] newChar = new char[]{chinese.toCharArray()[0]};  //转为单个字符HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);for (int i = 0; i < newChar.length; i++) {if (newChar[i] > 128) {try {pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0);} catch (BadHanyuPinyinOutputFormatCombination e) {e.printStackTrace();}} else {pinyinStr += newChar[i];}}return pinyinStr;}/*** 汉字转为拼音** @param chinese* @return*/public static String ToPinyin(String chinese) {String pinyinStr = "";char[] newChar = chinese.toCharArray();HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);for (int i = 0; i < newChar.length; i++) {if (newChar[i] > 128) {try {pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];} catch (BadHanyuPinyinOutputFormatCombination e) {e.printStackTrace();}} else {pinyinStr += newChar[i];}}return pinyinStr;}}

批量更改数据sql

<update id="updateDataById" parameterType="java.util.List"><foreach collection="list" item="item" separator=";">update platform_address set initials=#{item.initials},complete_Spelling = #{item.completeSpelling}where id =#{item.id}</foreach>
</update>

数据库连接 必须配置,否则不能批量更改,以下是参考链接

https://blog.csdn.net/carbuser_xl/article/details/127045359

 url: jdbc:mysql://rm:5888/first_pet_dev?autoReconnect=true&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false&allowMultiQueries=true

拼音依赖

  <dependency><groupId>com.belerweb</groupId><artifactId>pinyin4j</artifactId><version>2.5.0</version></dependency>

线程池工具类

import org.apache.tomcat.util.threads.ThreadPoolExecutor;import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;/*** 描述:* 线程池工具类* - 所有线程的创建与使用请调用此类的方法** @author zhaofeng* @date 2023-08-29*/
public class ThreadPoolUtils {/*** http异步请求* 耗时相对较长,取核心数*2* 阻塞时间60秒* 空闲时间超过60秒后销毁线程*/public static final ThreadPoolExecutor httpThreadPool = new ThreadPoolImpl(8, 16, 60, TimeUnit.SECONDS,new LinkedBlockingQueue<>(1024), new ThreadPoolExecutor.AbortPolicy());/*** 数据库操作请求* 相较http请求耗时较短* 取核心线程数*1* 阻塞时间60秒*/public static final ThreadPoolExecutor sqlThreadPool = new ThreadPoolImpl(4, 8, 60, TimeUnit.SECONDS,new LinkedBlockingQueue<>(1024), new ThreadPoolExecutor.AbortPolicy());}
http://www.lryc.cn/news/183485.html

相关文章:

  • 910数据结构(2019年真题)
  • 推荐系统实践 笔记
  • 【JavaEE】JUC(Java.util.concurrent)常见类
  • 清除浮动的方法
  • LangChain 摘要 和问答示例
  • (32)测距仪(声纳、激光雷达、深度摄影机)
  • 教你拥有一个自己的QQ机器人!0基础超详细保姆级教学!基于NoneBot2 Windows端搭建QQ机器人
  • 智能银行卡明细筛选与统计,轻松掌握账户总花销!
  • SRT服务器SLS
  • Linux 安装 Android SDK
  • 【QT开发笔记-基础篇】| 第四章 事件QEvent | 4.4 鼠标按下、移动、释放事件
  • vue3父子通信+ref,toRef,toRefs使用实例
  • 输入电压转化为电流性 5~20mA方案
  • SpringBoot自带模板引擎Thymeleaf使用详解①
  • 推荐算法——Apriori算法原理
  • vue ant 隐藏 列
  • java基础之初始化顺序
  • FFmpeg 命令:从入门到精通 | ffmpeg filter(过滤器 / 滤镜)
  • 【C语言】23-结构体类型
  • Python小技巧:快速合并字典dict()
  • 如何使用 React 和 Docusaurus 编写的一些自定义钩子(Hook)
  • 【初识Linux】Linux环境配置、Linux的基本指令 一
  • conda常用命令参数,指定版本,依赖库文件,创建虚拟环境,删除,激活,退出,内部安装包,pip通过代理安装包
  • 【锁的区别】C++线程库和POSIX线程库锁的区别
  • 网络层·IP协议
  • RabbitMQ学习笔记(下):延迟队列,发布确认高级,备份交换机
  • Python 无废话-基础知识面向对象编程详解
  • 凉鞋的 Unity 笔记 106. 第二轮循环场景视图Sprite Renderer
  • Vue中如何进行分布式路由配置与管理
  • Solidity 合约漏洞,价值 38BNB 漏洞分析