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 的 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行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!