简单网站开发项目实例企业网址是怎么写的

news/2025/10/3 22:00:56/文章来源:
简单网站开发项目实例,企业网址是怎么写的,制作网站教程,wordpress建单页面论坛提起SimpleDateFormat类#xff0c;想必做过Java开发的童鞋都不会感到陌生。没错#xff0c;它就是Java中提供的日期时间的转化类。这里#xff0c; 为什么说SimpleDateFormat类有线程安全问题呢#xff1f;有些小伙伴可能会提出疑问#xff1a;我们生产环境上一直在使用S…提起SimpleDateFormat类想必做过Java开发的童鞋都不会感到陌生。没错它就是Java中提供的日期时间的转化类。这里 为什么说SimpleDateFormat类有线程安全问题呢有些小伙伴可能会提出疑问我们生产环境上一直在使用SimpleDateFormat 类来解析和格式化日期和时间类型的数据一直都没有问题啊我的回答是没错那是因为你们的系统达不到 SimpleDateFormat类出现问题的并发量也就是说你们的系统没啥负载 接下来我们就一起看下在高并发下SimpleDateFormat类为何会出现安全问题以及如何解决SimpleDateFormat类的安全问 题。 重现SimpleDateFormat线程安全问题 public class SimpleDateFormatTest01 {//执行总次数private static final int EXECUTE_COUNT 1000;//同时运行的线程数量private static final int THREAD_COUNT 20;//SimpleDateFormat对象private static SimpleDateFormat simpleDateFormat new SimpleDateFormat(yyyy-MM-dd);public static void main(String[] args) throws InterruptedException {final Semaphore semaphore new Semaphore(THREAD_COUNT);final CountDownLatch countDownLatch new CountDownLatch(EXECUTE_COUNT);ExecutorService executorService Executors.newCachedThreadPool();for (int i 0; i EXECUTE_COUNT; i) {executorService.execute(() - {try {semaphore.acquire();try {simpleDateFormat.parse(2020-01-01);} catch (ParseException e) {System.out.println(线程 Thread.currentThread().getName() 格式化日期失败);e.printStackTrace();System.exit(1);} catch (NumberFormatException e) {System.out.println(线程 Thread.currentThread().getName() 格式化日期失败);e.printStackTrace();System.exit(1);}semaphore.release();} catch (InterruptedException e) {System.out.println(信号量发生错误);e.printStackTrace();System.exit(1);}countDownLatch.countDown();});}countDownLatch.await();executorService.shutdown();System.out.println(所有线程格式化日期成功);} }运行结果 java.lang.NumberFormatException: For input string: 线程pool-1-thread-1 格式化日期失败at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)at java.lang.Long.parseLong(Long.java:601)at java.lang.Long.parseLong(Long.java:631)at java.text.DigitList.getLong(DigitList.java:195)at java.text.DecimalFormat.parse(DecimalFormat.java:2051)at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869)at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514)at java.text.DateFormat.parse(DateFormat.java:364)at com.xysd.concurrent.lab06.SimpleDateFormatTest01.lambda$main$0(SimpleDateFormatTest01.java:72)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)at java.lang.Thread.run(Thread.java:748)原因 //SimpleDateFormat public Date parse(String text, ParsePosition pos) {CalendarBuilder calb new CalendarBuilder();Date parsedDate;try {//TODOparsedDate calb.establish(calendar).getTime();if (ambiguousYear[0]) {if (parsedDate.before(defaultCenturyStart)) {parsedDate calb.addYear(100).establish(calendar).getTime();}}}catch (IllegalArgumentException e) {pos.errorIndex start;pos.index oldStart;return null;}return parsedDate;}//CalendarBuilder //clear()和set()这两个方法执行不是原子操作所以造成线程不安全 Calendar establish(Calendar cal) {boolean weekDate isSet(WEEK_YEAR) field[WEEK_YEAR] field[YEAR];if (weekDate !cal.isWeekDateSupported()) {// Use YEAR insteadif (!isSet(YEAR)) {set(YEAR, field[MAX_FIELD WEEK_YEAR]);}weekDate false;}//这里线clearcal.clear();for (int stamp MINIMUM_USER_STAMP; stamp nextStamp; stamp) {for (int index 0; index maxFieldIndex; index) {if (field[index] stamp) {//然后再setcal.set(index, field[MAX_FIELD index]);break;}}}if (weekDate) {int weekOfYear isSet(WEEK_OF_YEAR) ? field[MAX_FIELD WEEK_OF_YEAR] : 1;int dayOfWeek isSet(DAY_OF_WEEK) ?field[MAX_FIELD DAY_OF_WEEK] : cal.getFirstDayOfWeek();if (!isValidDayOfWeek(dayOfWeek) cal.isLenient()) {if (dayOfWeek 8) {dayOfWeek--;weekOfYear dayOfWeek / 7;dayOfWeek (dayOfWeek % 7) 1;} else {while (dayOfWeek 0) {dayOfWeek 7;weekOfYear--;}}dayOfWeek toCalendarDayOfWeek(dayOfWeek);}cal.setWeekDate(field[MAX_FIELD WEEK_YEAR], weekOfYear, dayOfWeek);}return cal;}解决方案一 方法局部变量 public class SimpleDateFormatTest01 {//执行总次数private static final int EXECUTE_COUNT 1000;//同时运行的线程数量private static final int THREAD_COUNT 20;public static void main(String[] args) throws InterruptedException {final Semaphore semaphore new Semaphore(THREAD_COUNT);final CountDownLatch countDownLatch new CountDownLatch(EXECUTE_COUNT);ExecutorService executorService Executors.newCachedThreadPool();for (int i 0; i EXECUTE_COUNT; i) {executorService.execute(() - {try {semaphore.acquire();try {SimpleDateFormat simpleDateFormat new SimpleDateFormat(yyyy-MM-dd);simpleDateFormat.parse(2020-01-01);} catch (ParseException e) {System.out.println(线程 Thread.currentThread().getName() 格式化日期失败);e.printStackTrace();System.exit(1);} catch (NumberFormatException e) {System.out.println(线程 Thread.currentThread().getName() 格式化日期失败);e.printStackTrace();System.exit(1);}semaphore.release();} catch (InterruptedException e) {System.out.println(信号量发生错误);e.printStackTrace();System.exit(1);}countDownLatch.countDown();});}countDownLatch.await();executorService.shutdown();System.out.println(所有线程格式化日期成功);} }解决方案二 使用synchronized锁 public class SimpleDateFormatTest01 {//执行总次数private static final int EXECUTE_COUNT 1000;//同时运行的线程数量private static final int THREAD_COUNT 20;private static SimpleDateFormat simpleDateFormat new SimpleDateFormat(yyyy-MM-dd);public static void main(String[] args) throws InterruptedException {final Semaphore semaphore new Semaphore(THREAD_COUNT);final CountDownLatch countDownLatch new CountDownLatch(EXECUTE_COUNT);ExecutorService executorService Executors.newCachedThreadPool();for (int i 0; i EXECUTE_COUNT; i) {executorService.execute(() - {try {semaphore.acquire();try {synchronized(simpleDateFormat){simpleDateFormat.parse(2020-01-01);}} catch (ParseException e) {System.out.println(线程 Thread.currentThread().getName() 格式化日期失败);e.printStackTrace();System.exit(1);} catch (NumberFormatException e) {System.out.println(线程 Thread.currentThread().getName() 格式化日期失败);e.printStackTrace();System.exit(1);}semaphore.release();} catch (InterruptedException e) {System.out.println(信号量发生错误);e.printStackTrace();System.exit(1);}countDownLatch.countDown();});}countDownLatch.await();executorService.shutdown();System.out.println(所有线程格式化日期成功);} }解决方案三 Lock锁 public class SimpleDateFormatTest01 {//执行总次数private static final int EXECUTE_COUNT 1000;//同时运行的线程数量private static final int THREAD_COUNT 20;private static SimpleDateFormat simpleDateFormat new SimpleDateFormat(yyyy-MM-dd);private static Lock lock new ReentrackLock();public static void main(String[] args) throws InterruptedException {final Semaphore semaphore new Semaphore(THREAD_COUNT);final CountDownLatch countDownLatch new CountDownLatch(EXECUTE_COUNT);ExecutorService executorService Executors.newCachedThreadPool();for (int i 0; i EXECUTE_COUNT; i) {executorService.execute(() - {try {semaphore.acquire();try {lock.lock();simpleDateFormat.parse(2020-01-01);} catch (ParseException e) {System.out.println(线程 Thread.currentThread().getName() 格式化日期失败);e.printStackTrace();System.exit(1);} catch (NumberFormatException e) {System.out.println(线程 Thread.currentThread().getName() 格式化日期失败);e.printStackTrace();System.exit(1);}finally{lock.unlock();}semaphore.release();} catch (InterruptedException e) {System.out.println(信号量发生错误);e.printStackTrace();System.exit(1);}countDownLatch.countDown();});}countDownLatch.await();executorService.shutdown();System.out.println(所有线程格式化日期成功);} }解决方案四 ThreadLocal public class SimpleDateFormatTest01 {//执行总次数private static final int EXECUTE_COUNT 1000;//同时运行的线程数量private static final int THREAD_COUNT 20;//SimpleDateFormat对象private static SimpleDateFormat simpleDateFormat new SimpleDateFormat(yyyy-MM-dd);//本地线程private static ThreadLocalSimpleDateFormat threadLocal new ThreadLocalSimpleDateFormat() {Overrideprotected SimpleDateFormat initialValue() {return new SimpleDateFormat(yyyy-MM-dd);}};private static DateFormat getDateFormat() {SimpleDateFormat dateFormat threadLocal.get();if (dateFormat null) {dateFormat new SimpleDateFormat(yyyy-MM-dd);threadLocal.set(dateFormat);}return dateFormat;}public static void main(String[] args) throws InterruptedException {final Semaphore semaphore new Semaphore(THREAD_COUNT);final CountDownLatch countDownLatch new CountDownLatch(EXECUTE_COUNT);ExecutorService executorService Executors.newCachedThreadPool();for (int i 0; i EXECUTE_COUNT; i) {executorService.execute(() - {try {semaphore.acquire();try {getDateFormat().parse(2020-01-01);} catch (ParseException e) {System.out.println(线程 Thread.currentThread().getName() 格式化日期失败);e.printStackTrace();System.exit(1);} catch (NumberFormatException e) {System.out.println(线程 Thread.currentThread().getName() 格式化日期失败);e.printStackTrace();System.exit(1);}semaphore.release();} catch (InterruptedException e) {System.out.println(信号量发生错误);e.printStackTrace();System.exit(1);}countDownLatch.countDown();});}countDownLatch.await();executorService.shutdown();System.out.println(所有线程格式化日期成功);}}解决方案五 DateTimeFormatter public class SimpleDateFormatTest01 {//执行总次数private static final int EXECUTE_COUNT 1000;//同时运行的线程数量private static final int THREAD_COUNT 20;private static DateTimeFormatter formatter DateTimeFormatter.ofPattern(yyyy-MM-dd);public static void main(String[] args) throws InterruptedException {final Semaphore semaphore new Semaphore(THREAD_COUNT);final CountDownLatch countDownLatch new CountDownLatch(EXECUTE_COUNT);ExecutorService executorService Executors.newCachedThreadPool();for (int i 0; i EXECUTE_COUNT; i) {executorService.execute(() - {try {semaphore.acquire();try {LocalDate.parse(2020-01-01, formatter);} catch (Exception e) {System.out.println(线程 Thread.currentThread().getName() 格式化日期失败);e.printStackTrace();System.exit(1);}semaphore.release();} catch (InterruptedException e) {System.out.println(信号量发生错误);e.printStackTrace();System.exit(1);}countDownLatch.countDown();});}countDownLatch.await();executorService.shutdown();System.out.println(所有线程格式化日期成功);} }总结 在高并发情况下推荐使用ThreadLocal或DateTimeFormatter

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/926369.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

网站搭建多少钱徐州百都网络非常好网站建设代码怎么导入图片

我一直不知道我在大家心目中的定位是什么,但我内心其实是把自己定义为一个『工具人』的。可能是因为我自己本身就是程序员,所以更能理解程序员的不易吧。所以,我尽量不写水文,只分享干货。就是希望大家看了能够有所收获&#xff0…

从DQN到Double DQN:分离动作选择与价值评估,解决强化学习中的Q值过估计问题

2015年DQN在Atari游戏上取得突破性进展,从此以后强化学习终于能处理复杂环境了,但没多久研究者就注意到一些奇怪的现象: Q值会莫名其妙地增长到很大,智能体变得异常自信,坚信某些动作价值极高。实际跑起来却发现这…

P9877/QOJ5069 Vacation

题意 给定长度为 \(n\) 的序列 \(a\) 和定值 \(c\),\(q\) 次操作,每次操作可以是单点修改,也可以是查询 \([l,r]\) 的所有区间长度 \(\le c\) 的子区间中区间和最大是多少。 \(c\le n\le 2\times10^5,q\le 5\times1…

CF1916G Optimizations From Chelsu

神仙题,神仙做法。点分治,假设路径的端点是 \(s\) 和 \(t\),那么 \(len\times g\) 就是 \((d_s+d_t)\times \gcd(v_s,v_t)\),其中 \(d\) 是到根链长度,\(v\) 是到根的 \(\gcd\)。 不妨设 \(d_s\ge d_t\),那么 \(…

详细介绍:微服务架构:基于Spring Cloud ,构建同城生活服务平台

详细介绍:微服务架构:基于Spring Cloud ,构建同城生活服务平台pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "…

【游记】北京师范大学讲课

【游记】北京师范大学讲课好久没有写长文了,今天很有兴致,写一篇罢! ccpc 网络赛前一天 去北京参加 tower research 举办的宣讲会。结束后和 yzf 找芈重山吃,海淀桥店人满为患,于是跑到了五道口,发现也是人满为患…

做网站 带宽 多少钱厦门国外网站建设公司哪家好

一、按键无效现象1.操作人员对机床操作时画面无反应现象M80/M800系列系统在使用键盘或触摸屏输入时,请勿连击键盘按键,输入一 次即可。键盘每向NC输入一次,NC系统需要计算一次,连续的多次重复输入,NC多次重复计算、显示…

网站建设步骤与时间表个人定做衣服店

提示:本文是我cuda教程部分代码和内容构成,严禁侵权! 文章目录 前言一、核函数index寻找1、3d grid与1d block索引2、1d grid, 2d block索引二、kernel函数实例三、性能优化(内存)四、原子操作五、流stream六、cuda处理nms编码七、cuda处理yolo算法输出编码八、cuda处理yolo…

房产交易网站开发三网合一网站源码下载

本系列第三篇文章,一起了解下PSR规范中的PSR4和PSR0规范首先恭喜大家,包括我自己,坚持到了现在。这篇文章之后,Composer的基础原理就清晰明了咯。也就是说,Composer所利用的正是spl_autoload_register()和PSR4规范&…

青浦网站制作su35湘潭做网站 i磐石网络

9.1 类(class)面向对象最重要的概念就是类(Class)和实例(Instance),类是抽象的模板以Student类为例,在Python中,定义类是通过class关键字class后面紧接着是类名,即Student,类名通常是大写开头的单词,紧接着…

ARM芯片架构之DAP:AXI-AP 技术详解 - 实践

ARM芯片架构之DAP:AXI-AP 技术详解 - 实践pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "…

备案中的网站名称重庆百度搜索排名优化

Python实战:打造自定义随机验证码生成器 在许多网站中,验证码作为一种安全机制被广泛采用,用于验证用户身份、防止自动化攻击和确保数据安全。本篇教程将带领您一步步使用Python创建一个功能齐全的随机验证码生成器。我们将通过导入必要的库…

详细介绍:代码世界的“数字刑侦”:深入解析代码审计实战

详细介绍:代码世界的“数字刑侦”:深入解析代码审计实战pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consola…

三霍尔BLDC如何测量Hall同步角度(需要示波器)

现在电机转子位置估计的研究有两种主流技术路线——一种是无传感器,另一种就是采用低分辨率传感器估计高精度位置。当时我(知乎:土豆泥)本科毕业答辩做的就是霍尔传感器。其实可以看到,近几年业界越来越多的FOC驱…

完整教程:K8s学习笔记(十) Deployment 副本控制器

完整教程:K8s学习笔记(十) Deployment 副本控制器pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas"…

湖南建设信息网站做手机版网站和做app差别

接口测试虽然作为版本的一环,但是也是有一套完整的体系,有接口的功能测试、性能测试、安全测试;同时,由于接口的特性,接口的自动化低成本高收益的,使用一些开源工具或一些轻量级的方法,在测试用…

QBXT2025S刷题 Day2

今天的题目 \(rk38\) T1 这道题想了 \(1h\) 差不多。 这道题其实可以直接转化成选一个点,把覆盖着这个点线段全部删掉,使得左右两边都有线段。 可以维护每个点被多少个区间覆盖,左面有多少个区间,右面有多少个区间…

个人主页网址

https://www.cnblogs.com/gutianle

建设项目环境影响评价公示网站wordpress添加干扰代码

数据可视化是将复杂数据转化为易于理解的图表和图形的过程,帮助我们发现趋势、关联和模式。同时数据可视化也是数字孪生的基础,本文小编带大家用最简单的话语为大家讲解怎么制作一个数据可视化大屏,接下来跟随小编的思路走起来~ 1.数据收集和…

离型剂技术支持东莞网站建设php 移动网站开发

1025. 反转链表 (25) 时间限制300 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者CHEN, Yue给定一个常数K以及一个单链表L,请编写程序将L中每K个结点反转。例如:给定L为1→2→3→4→5→6,K为3,则输出应该为3→2→1→6…