在多线程中输出时间戳是否会重复
在多线程中输出时间戳是否会重复
在多线程中,以时间戳为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位的微秒为单位的.
在多线程中输出时间戳是否会重复相关推荐
- 下面的程序可以从0....n-1中随机等概率的输出m个不重复的数。(假设nm)
问题:题目下面的程序可以从0....n-1中随机等概率的输出m个不重复的数.这里我们假设n远大于m. 括号里应填写什么? ....... knuth(int n, int m) { sran ...
- 编写一个Java程序实现多线程,在线程中输出线程的名字,隔300毫秒输出一次,共输出20次。
编写一个Java程序实现多线程,在线程中输出线程的名字,隔300毫秒输出一次,共输出20次. 一.ThreadDemo class ThreadDemo extends Thread {public ...
- linux系统如何查看是否是线程死锁,多线程中如何使用gdb精确定位死锁问题
本文转载自微信公众号「程序喵大人」,作者程序喵大人 .转载本文请联系程序喵大人公众号. 在多线程开发过程中很多人应该都会遇到死锁问题,死锁问题也是面试过程中经常被问到的问题,这里介绍在c++中如何使用 ...
- 线程中如何使用对象_多线程中如何使用gdb精确定位死锁问题
在多线程开发过程中很多人应该都会遇到死锁问题,死锁问题也是面试过程中经常被问到的问题,这里介绍在c++中如何使用gdb+python脚本调试死锁问题,以及如何在程序运行过程中检测死锁. 首先介绍什么是 ...
- python sleep和wait_多线程中sleep()、wait()方法等的区别
1.这两个方法来自不同的类分别是Thread和Object 2.最主要是sleep方法没有释放锁,而wait方法释放了锁,使得其他线程可以使用同步控制块或者方法. 3.wait,notify和noti ...
- 多线程中Thread的join方法
多线程中Thread的join方法 join简介 join方法是Thread类中的一个方法,该方法的定义是等待该线程执行直到终止.其实就说join方法将挂起调用线程的执行,直到被调用的对象完成它的执行 ...
- 【Linux】多线程中使用fork()
(最核心的东西我在下面用红色字体标出来了,理解了那块,这些东西都是就理解了!) 在本篇文章开始之前,需要大家先了解线程和进程,这位大哥讲的言简意赅:进程和线程的主要区别(总结)_kuangsongha ...
- android串口补位,Rust多线程中的消息传递机制
代码说话. use std::thread; use std::sync::mpsc; use std::time::Duration; fn main() { let (tx, rx) = mpsc ...
- 整数数组查找java_使用Java编写程序以查找整数数组中的第一个非重复数字?
查找数组中的第一个非重复数字-构造count数组以将给定数组中每个元素的计数存储为相同长度,且所有元素的初始值为0. 将数组中的每个元素与除自身之外的所有其他元素进行比较. 如果匹配发生,则增加其在计 ...
最新文章
- 2022-2028年中国儿童保健品行业市场研究及前瞻分析报告
- js基础知识温习:Javascript中如何模拟私有方法
- java tessbaseapi,T+开发者社区
- 技术网站 --人人都是产品经理
- mysql union 优化_mysql 5.7.3 对union all 的优化
- Qt中的QFontDialog
- php识别号码格式豹子,[转载]php新手入门之PHP常用特殊运算符号
- exp-imp实现oracle不同表空间的迁移
- 华为手机怎么隐藏按键图标_华为手机隐藏技巧,一键简单设置,让沟通更加便捷...
- windows下部署免费ssl证书(letsencrypt)
- faster rcnn resnet_RCNN系列、Fast-RCNN、Faster-RCNN、R-FCN检测模型对比
- DNE-1 测试方法
- [jQuery]30+ Brand New jQuery Plugins To Change the Look and Feel of Your Website
- 机器人动力学建模之牛顿欧拉法推导
- 自定义VB程序加密方案
- modelica用inertia连接FlangeWithBearing时报组件不匹配连接错误:incompatible components in connect statement
- 前端——面试(苏小妍)
- 鼠标移上去变小手样式
- openpyxl给excel设置条件格式
- Thrift入门学习