浅谈 SimpleDateFormat,第三方库joda-time,JDK8提供时间类 之性能和线程安全

ExecutorService executorService = Executors.newFixedThreadPool(CLIENT_COUNT);

// 控制同一个时刻,只能有多少个线程同时运行指定代码,即acquire和release之间的代码

Semaphore semaphore = new Semaphore(THREAD_COUNT);

CountDownLatch countDownLatch = new CountDownLatch(CLIENT_COUNT);

for (int i = 0; i < CLIENT_COUNT; i++) {

executorService.execute(() -> {

try {

semaphore.acquire();

simpleDateFormat.parse(“20200917”);

semaphore.release();

} catch (InterruptedException e) {

e.printStackTrace();

} catch (ParseException e) {

e.printStackTrace();

} finally {

countDownLatch.countDown();

}

});

}

// 等待所有请求执行完

countDownLatch.await();

// 关闭线程池

executorService.shutdown();

}

}

在这里插入图片描述

线程安全写法:


package com.nobody.part02;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.concurrent.CountDownLatch;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Semaphore;

/**

  • @author Mr.nobody

  • @Description

  • @date 2020/9/17

*/

public class SimpleDateFormatDemo {

// 请求总数

private static int CLIENT_COUNT = 10000;

// 并发线程数

private static int THREAD_COUNT = 500;

// 全局变量,多线程访问会线程不安全,抛异常

//private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat(“yyyyMMdd”);

public static void main(String[] args) throws InterruptedException {

// 固定线程池

ExecutorService executorService = Executors.newFixedThreadPool(CLIENT_COUNT);

// 控制同一个时刻,只能有多少个线程同时运行指定代码,即acquire和release之间的代码

Semaphore semaphore = new Semaphore(THREAD_COUNT);

CountDownLatch countDownLatch = new CountDownLatch(CLIENT_COUNT);

for (int i = 0; i < CLIENT_COUNT; i++) {

executorService.execute(() -> {

try {

semaphore.acquire();

// 局部变量,线程安全

SimpleDateFormat simpleDateFormat = new SimpleDateFormat(“yyyyMMdd”);

simpleDateFormat.parse(“20200917”);

semaphore.release();

} catch (InterruptedException e) {

e.printStackTrace();

} catch (ParseException e) {

e.printStackTrace();

} finally {

countDownLatch.countDown();

}

});

}

// 等待所有请求执行完

countDownLatch.await();

// 关闭线程池

executorService.shutdown();

}

}

在这里插入图片描述

二、joda-time

======================================================================

第三方库 joda-time 的 DateTimeFormatter 类是线程安全的。

package com.nobody.part02;

import org.joda.time.DateTime;

import org.joda.time.format.DateTimeFormat;

import org.joda.time.format.DateTimeFormatter;

import java.util.concurrent.CountDownLatch;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Semaphore;

/**

  • @author Mr.nobody

  • @Description

  • @date 2020/9/17

*/

public class JodaTimeDemo {

// 请求总数

private static int CLIENT_COUNT = 10000;

// 并发线程数

private static int THREAD_COUNT = 500;

// 全局变量,线程安全

private static DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(“yyyyMMdd”);

public static void main(String[] args) throws InterruptedException {

// 固定线程池

ExecutorService executorService = Executors.newFixedThreadPool(CLIENT_COUNT);

// 控制同一个时刻,只能有多少个线程同时运行指定代码,即acquire和release之间的代码

Semaphore semaphore = new Semaphore(THREAD_COUNT);

CountDownLatch countDownLatch = new CountDownLatch(CLIENT_COUNT);

for (int i = 0; i < CLIENT_COUNT; i++) {

executorService.execute(() -> {

try {

semaphore.acquire();

DateTime.parse(“20200917”, dateTimeFormatter).toDate();

semaphore.release();

} catch (InterruptedException e) {

e.printStackTrace();

} finally {

countDownLatch.countDown();

}

});

}

// 等待所有请求执行完

countDownLatch.await();

// 关闭线程池

executorService.shutdown();

}

}

给大家的福利

零基础入门

对于从来没有接触过网络安全的同学,我们帮你准备了详细的学习成长路线图。可以说是最科学最系统的学习路线,大家跟着这个大的方向学习准没问题。

同时每个成长路线对应的板块都有配套的视频提供:

在这里插入图片描述

因篇幅有限,仅展示部分资料

网络安全面试题

绿盟护网行动

还有大家最喜欢的黑客技术

网络安全源码合集+工具包

所有资料共282G,朋友们如果有需要全套《网络安全入门+黑客进阶学习资源包》,可以扫描下方二维码领取(如遇扫码问题,可以在评论区留言领取哦)~

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以点击这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值