您的位置:首页 > 游戏 > 游戏 > 单线程环境下,用时间做为单一文件名称全局唯一处理

单线程环境下,用时间做为单一文件名称全局唯一处理

2025/2/13 6:05:11 来源:https://blog.csdn.net/weixin_66196770/article/details/141473932  浏览:    关键词:单线程环境下,用时间做为单一文件名称全局唯一处理
package org.example.file;import java.util.concurrent.ConcurrentHashMap;public class FileHelper {private FileService fileService;//全局唯一,用于控制文件时间全局唯一//value:0:没被消费,1:被消费public static final ConcurrentHashMap<Long, Integer> MONITOR_MAP = new ConcurrentHashMap<>();public Boolean checkTimeSingle(){int i = 0;while (i != 100) {//当前线程时间Long currentTime = System.currentTimeMillis();if (MONITOR_MAP.containsKey(currentTime)) {//判断是否被消费if (MONITOR_MAP.get(currentTime) == 0) {if (fileService.saveFile()) {//如果消费成功改为1并删除MONITOR_MAP.put(currentTime, 1);MONITOR_MAP.remove(currentTime);return true;} else {return false;}}} else {MONITOR_MAP.put(currentTime, 0);i++;}}return false;}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com