在多线程中输出时间戳是否会重复

在多线程中,以时间戳为key,存入map,使用完毕后删除.会不会报错. 会
因为使用System.currentTimeMillis()的结果是13位的,属于(毫秒级别),但是多线程的执行会小于毫秒,会造成多个线程中,产生的时间戳是一样的.
建议使用uuid或者 System.nanoTime()

线程池配置

package com.cloudskysec.operation.config;import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
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 javax.annotation.PreDestroy;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;/****/
@Configuration
@EnableAsync
public class NewThreadPoolConfig {@Bean("xyqThreadPoolTaskExecutor")@ConditionalOnMissingBean(name = "xyqThreadPoolTaskExecutor")public Executor xyqThreadPoolTaskExecutor() {System.out.println("==================xyqThreadPoolTaskExecutor开启==================");ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();// 核心线程池大小executor.setCorePoolSize(10);// 最大线程池大小executor.setMaxPoolSize(50);// 队列容量executor.setQueueCapacity(100);// 线程池维护线程所允许的空闲时间executor.setKeepAliveSeconds(60);// 线程名称前缀executor.setThreadNamePrefix("xyq-");// 拒绝策略executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());// 初始化线程池executor.initialize();return executor;}@PreDestroypublic void shutdownThreadPoolExecutor() {ThreadPoolTaskExecutor executor = (ThreadPoolTaskExecutor) xyqThreadPoolTaskExecutor();executor.shutdown();System.out.println("==================xyqThreadPoolTaskExecutor关闭==================");}
}

业务使用

可以做一个测试业务来看看

package com.cloudskysec.operation.controller;import com.cloudskysec.commons.core.core.domain.AjaxResult;
import com.cloudskysec.commons.core.utils.UUIDUtils;
import com.cloudskysec.operation.utils.ExecUtils;
import com.jcraft.jsch.JSchException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;/*** @author : xuyanqiang* @date : 2023/4/17*/
@RestController
@RequestMapping("/TestControllerII")
public class TestControllerII {@Autowired@Qualifier("xyqThreadPoolTaskExecutor")private Executor xyqThreadPoolTaskExecutor;/*** 存放ssh连接信息的map*/public static Map<String, Object> execSshMap = new ConcurrentHashMap<>();@GetMapping("/testMethodI")public AjaxResult testMethodI(@RequestParam Map<String, Object> param) throws Exception {String codeValue = (String) param.get("codeValue");Map<String, Object> resultMap = this.testMethodCommon(codeValue);return AjaxResult.error("OK", resultMap);}@Async("xyqThreadPoolTaskExecutor")public Map<String, Object> testMethodCommon(String codeValue) throws Exception {Map<String, Object> resultMap = new HashMap<>();int subThreadNum = 10;CountDownLatch countDownLatch = new CountDownLatch(subThreadNum);for (int i = 0; i < subThreadNum; i++) {String uuid = UUIDUtils.getUUID();xyqThreadPoolTaskExecutor.execute(() -> {String Key = System.currentTimeMillis() + "";System.out.println(Key);// 添加execSshMap.put(Key, codeValue + uuid);// 等待try {Thread.sleep(2000);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(execSshMap.toString());countDownLatch.countDown();});}countDownLatch.await(6000, TimeUnit.SECONDS);return resultMap;}
}
1682409421290
1682409421290
1682409421290
1682409421290
1682409421290
1682409421290
1682409421290
1682409421290
1682409421290
1682409421290
{1682409421290=3232323236188d79f48f40c4948a2d0c9f5e82ce}
{1682409421290=3232323236188d79f48f40c4948a2d0c9f5e82ce}
{1682409421290=3232323236188d79f48f40c4948a2d0c9f5e82ce}
{1682409421290=3232323236188d79f48f40c4948a2d0c9f5e82ce}
{1682409421290=3232323236188d79f48f40c4948a2d0c9f5e82ce}
{1682409421290=3232323236188d79f48f40c4948a2d0c9f5e82ce}
{1682409421290=3232323236188d79f48f40c4948a2d0c9f5e82ce}
{1682409421290=3232323236188d79f48f40c4948a2d0c9f5e82ce}
{1682409421290=3232323236188d79f48f40c4948a2d0c9f5e82ce}
{1682409421290=3232323236188d79f48f40c4948a2d0c9f5e82ce}

造成这样的原因:
因为在多线程中使用的时间戳是一样的,key也就一样了.在等待的两秒.value值就由最后一个代替了.如果我们的value是封装对象的话,就会造成对象错误.
解决:

// String Key = System.currentTimeMillis() + "";String Key = System.nanoTime() + "";或者String Key = UUIDUtils.getUUID();
257968451921600
257968451958600
257968452054200
257968451859700
257968451857600
257968452028600
257968451992900
257968451777900
257968451948800
257968451765600
{257968452028600=32323232a882b12f4e78441788d85305ca207823, 257968451777900=3232323232ae1cf611714c8693425e5206584374, 257968451765600=3232323270357746ac2248c1b92bdef458945104, 257968451958600=32323232909868a86c3e44cfacc0bdb02d8e93fe, 1682409421290=3232323236188d79f48f40c4948a2d0c9f5e82ce, 257968451859700=323232323459d2eeb7b040eb90091dd8b46e26b3, 257968451857600=323232324db21b7bdbd74540a7e377e4201a38ed, 257968451921600=323232325dd811e648cf49a2a04a61849d46ce65, 257968452054200=323232324cb467aa39bb40169c31acb90658290b, 257968451992900=323232329c9edb74456040358bc1e1e04877ab2b, 257968451948800=323232324957703a33db44319f5970f2f32c2eb7}
{257968452028600=32323232a882b12f4e78441788d85305ca207823, 257968451777900=3232323232ae1cf611714c8693425e5206584374, 257968451765600=3232323270357746ac2248c1b92bdef458945104, 257968451958600=32323232909868a86c3e44cfacc0bdb02d8e93fe, 1682409421290=3232323236188d79f48f40c4948a2d0c9f5e82ce, 257968451859700=323232323459d2eeb7b040eb90091dd8b46e26b3, 257968451857600=323232324db21b7bdbd74540a7e377e4201a38ed, 257968451921600=323232325dd811e648cf49a2a04a61849d46ce65, 257968452054200=323232324cb467aa39bb40169c31acb90658290b, 257968451992900=323232329c9edb74456040358bc1e1e04877ab2b, 257968451948800=323232324957703a33db44319f5970f2f32c2eb7}
{257968452028600=32323232a882b12f4e78441788d85305ca207823, 257968451777900=3232323232ae1cf611714c8693425e5206584374, 257968451765600=3232323270357746ac2248c1b92bdef458945104, 257968451958600=32323232909868a86c3e44cfacc0bdb02d8e93fe, 1682409421290=3232323236188d79f48f40c4948a2d0c9f5e82ce, 257968451859700=323232323459d2eeb7b040eb90091dd8b46e26b3, 257968451857600=323232324db21b7bdbd74540a7e377e4201a38ed, 257968451921600=323232325dd811e648cf49a2a04a61849d46ce65, 257968452054200=323232324cb467aa39bb40169c31acb90658290b, 257968451992900=323232329c9edb74456040358bc1e1e04877ab2b, 257968451948800=323232324957703a33db44319f5970f2f32c2eb7}
{257968452028600=32323232a882b12f4e78441788d85305ca207823, 257968451777900=3232323232ae1cf611714c8693425e5206584374, 257968451765600=3232323270357746ac2248c1b92bdef458945104, 257968451958600=32323232909868a86c3e44cfacc0bdb02d8e93fe, 1682409421290=3232323236188d79f48f40c4948a2d0c9f5e82ce, 257968451859700=323232323459d2eeb7b040eb90091dd8b46e26b3, 257968451857600=323232324db21b7bdbd74540a7e377e4201a38ed, 257968451921600=323232325dd811e648cf49a2a04a61849d46ce65, 257968452054200=323232324cb467aa39bb40169c31acb90658290b, 257968451992900=323232329c9edb74456040358bc1e1e04877ab2b, 257968451948800=323232324957703a33db44319f5970f2f32c2eb7}
{257968452028600=32323232a882b12f4e78441788d85305ca207823, 257968451777900=3232323232ae1cf611714c8693425e5206584374, 257968451765600=3232323270357746ac2248c1b92bdef458945104, 257968451958600=32323232909868a86c3e44cfacc0bdb02d8e93fe, 1682409421290=3232323236188d79f48f40c4948a2d0c9f5e82ce, 257968451859700=323232323459d2eeb7b040eb90091dd8b46e26b3, 257968451857600=323232324db21b7bdbd74540a7e377e4201a38ed, 257968451921600=323232325dd811e648cf49a2a04a61849d46ce65, 257968452054200=323232324cb467aa39bb40169c31acb90658290b, 257968451992900=323232329c9edb74456040358bc1e1e04877ab2b, 257968451948800=323232324957703a33db44319f5970f2f32c2eb7}
{257968452028600=32323232a882b12f4e78441788d85305ca207823, 257968451777900=3232323232ae1cf611714c8693425e5206584374, 257968451765600=3232323270357746ac2248c1b92bdef458945104, 257968451958600=32323232909868a86c3e44cfacc0bdb02d8e93fe, 1682409421290=3232323236188d79f48f40c4948a2d0c9f5e82ce, 257968451859700=323232323459d2eeb7b040eb90091dd8b46e26b3, 257968451857600=323232324db21b7bdbd74540a7e377e4201a38ed, 257968451921600=323232325dd811e648cf49a2a04a61849d46ce65, 257968452054200=323232324cb467aa39bb40169c31acb90658290b, 257968451992900=323232329c9edb74456040358bc1e1e04877ab2b, 257968451948800=323232324957703a33db44319f5970f2f32c2eb7}
{257968452028600=32323232a882b12f4e78441788d85305ca207823, 257968451777900=3232323232ae1cf611714c8693425e5206584374, 257968451765600=3232323270357746ac2248c1b92bdef458945104, 257968451958600=32323232909868a86c3e44cfacc0bdb02d8e93fe, 1682409421290=3232323236188d79f48f40c4948a2d0c9f5e82ce, 257968451859700=323232323459d2eeb7b040eb90091dd8b46e26b3, 257968451857600=323232324db21b7bdbd74540a7e377e4201a38ed, 257968451921600=323232325dd811e648cf49a2a04a61849d46ce65, 257968452054200=323232324cb467aa39bb40169c31acb90658290b, 257968451992900=323232329c9edb74456040358bc1e1e04877ab2b, 257968451948800=323232324957703a33db44319f5970f2f32c2eb7}
{257968452028600=32323232a882b12f4e78441788d85305ca207823, 257968451777900=3232323232ae1cf611714c8693425e5206584374, 257968451765600=3232323270357746ac2248c1b92bdef458945104, 257968451958600=32323232909868a86c3e44cfacc0bdb02d8e93fe, 1682409421290=3232323236188d79f48f40c4948a2d0c9f5e82ce, 257968451859700=323232323459d2eeb7b040eb90091dd8b46e26b3, 257968451857600=323232324db21b7bdbd74540a7e377e4201a38ed, 257968451921600=323232325dd811e648cf49a2a04a61849d46ce65, 257968452054200=323232324cb467aa39bb40169c31acb90658290b, 257968451992900=323232329c9edb74456040358bc1e1e04877ab2b, 257968451948800=323232324957703a33db44319f5970f2f32c2eb7}
{257968452028600=32323232a882b12f4e78441788d85305ca207823, 257968451777900=3232323232ae1cf611714c8693425e5206584374, 257968451765600=3232323270357746ac2248c1b92bdef458945104, 257968451958600=32323232909868a86c3e44cfacc0bdb02d8e93fe, 1682409421290=3232323236188d79f48f40c4948a2d0c9f5e82ce, 257968451859700=323232323459d2eeb7b040eb90091dd8b46e26b3, 257968451857600=323232324db21b7bdbd74540a7e377e4201a38ed, 257968451921600=323232325dd811e648cf49a2a04a61849d46ce65, 257968452054200=323232324cb467aa39bb40169c31acb90658290b, 257968451992900=323232329c9edb74456040358bc1e1e04877ab2b, 257968451948800=323232324957703a33db44319f5970f2f32c2eb7}
{257968452028600=32323232a882b12f4e78441788d85305ca207823, 257968451777900=3232323232ae1cf611714c8693425e5206584374, 257968451765600=3232323270357746ac2248c1b92bdef458945104, 257968451958600=32323232909868a86c3e44cfacc0bdb02d8e93fe, 1682409421290=3232323236188d79f48f40c4948a2d0c9f5e82ce, 257968451859700=323232323459d2eeb7b040eb90091dd8b46e26b3, 257968451857600=323232324db21b7bdbd74540a7e377e4201a38ed, 257968451921600=323232325dd811e648cf49a2a04a61849d46ce65, 257968452054200=323232324cb467aa39bb40169c31acb90658290b, 257968451992900=323232329c9edb74456040358bc1e1e04877ab2b, 257968451948800=323232324957703a33db44319f5970f2f32c2eb7}

总结

这个问题虽然说是一个很简单的bug,但是排查的时候很难发现.
我们在线程中使用随机数时,最好的是使用uuid,不要使用时间戳.或者使用15位的微秒为单位的.

在多线程中输出时间戳是否会重复相关推荐

  1. 下面的程序可以从0....n-1中随机等概率的输出m个不重复的数。(假设nm)

    问题:题目下面的程序可以从0....n-1中随机等概率的输出m个不重复的数.这里我们假设n远大于m. 括号里应填写什么? ....... knuth(int n, int m) {      sran ...

  2. 编写一个Java程序实现多线程,在线程中输出线程的名字,隔300毫秒输出一次,共输出20次。

    编写一个Java程序实现多线程,在线程中输出线程的名字,隔300毫秒输出一次,共输出20次. 一.ThreadDemo class ThreadDemo extends Thread {public ...

  3. linux系统如何查看是否是线程死锁,多线程中如何使用gdb精确定位死锁问题

    本文转载自微信公众号「程序喵大人」,作者程序喵大人 .转载本文请联系程序喵大人公众号. 在多线程开发过程中很多人应该都会遇到死锁问题,死锁问题也是面试过程中经常被问到的问题,这里介绍在c++中如何使用 ...

  4. 线程中如何使用对象_多线程中如何使用gdb精确定位死锁问题

    在多线程开发过程中很多人应该都会遇到死锁问题,死锁问题也是面试过程中经常被问到的问题,这里介绍在c++中如何使用gdb+python脚本调试死锁问题,以及如何在程序运行过程中检测死锁. 首先介绍什么是 ...

  5. python sleep和wait_多线程中sleep()、wait()方法等的区别

    1.这两个方法来自不同的类分别是Thread和Object 2.最主要是sleep方法没有释放锁,而wait方法释放了锁,使得其他线程可以使用同步控制块或者方法. 3.wait,notify和noti ...

  6. 多线程中Thread的join方法

    多线程中Thread的join方法 join简介 join方法是Thread类中的一个方法,该方法的定义是等待该线程执行直到终止.其实就说join方法将挂起调用线程的执行,直到被调用的对象完成它的执行 ...

  7. 【Linux】多线程中使用fork()

    (最核心的东西我在下面用红色字体标出来了,理解了那块,这些东西都是就理解了!) 在本篇文章开始之前,需要大家先了解线程和进程,这位大哥讲的言简意赅:进程和线程的主要区别(总结)_kuangsongha ...

  8. android串口补位,Rust多线程中的消息传递机制

    代码说话. use std::thread; use std::sync::mpsc; use std::time::Duration; fn main() { let (tx, rx) = mpsc ...

  9. 整数数组查找java_使用Java编写程序以查找整数数组中的第一个非重复数字?

    查找数组中的第一个非重复数字-构造count数组以将给定数组中每个元素的计数存储为相同长度,且所有元素的初始值为0. 将数组中的每个元素与除自身之外的所有其他元素进行比较. 如果匹配发生,则增加其在计 ...

最新文章

  1. 2022-2028年中国儿童保健品行业市场研究及前瞻分析报告
  2. js基础知识温习:Javascript中如何模拟私有方法
  3. java tessbaseapi,T+开发者社区
  4. 技术网站 --人人都是产品经理
  5. mysql union 优化_mysql 5.7.3 对union all 的优化
  6. Qt中的QFontDialog
  7. php识别号码格式豹子,[转载]php新手入门之PHP常用特殊运算符号
  8. exp-imp实现oracle不同表空间的迁移
  9. 华为手机怎么隐藏按键图标_华为手机隐藏技巧,一键简单设置,让沟通更加便捷...
  10. windows下部署免费ssl证书(letsencrypt)
  11. faster rcnn resnet_RCNN系列、Fast-RCNN、Faster-RCNN、R-FCN检测模型对比
  12. DNE-1 测试方法
  13. [jQuery]30+ Brand New jQuery Plugins To Change the Look and Feel of Your Website
  14. 机器人动力学建模之牛顿欧拉法推导
  15. 自定义VB程序加密方案
  16. modelica用inertia连接FlangeWithBearing时报组件不匹配连接错误:incompatible components in connect statement
  17. 前端——面试(苏小妍)
  18. 鼠标移上去变小手样式
  19. openpyxl给excel设置条件格式
  20. Thrift入门学习

热门文章

  1. maven项目html文件打开乱码,maven项目文件乱码问题
  2. 实验二第1关:谁是小偷?
  3. 图像特征提取算法:方向梯度直方图HOG
  4. 盛天体育独家冠名|热烈祝贺2022年湖南体育设施行业“新产品新技术”推介会圆满召开
  5. 安卓硬编音视频数据推送到rtmp服务器
  6. python的文件格式有两种,*.py和*.pyw,它们有什么不同
  7. 记一次添加桌面二次元人物的经历
  8. SQL语句大全(详解)
  9. synchronized修饰方法
  10. 什么叫锭材综合成材率?如何计算?