From e0d793ba14b99e4dc3999e6852949a325c54171f Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sat, 13 May 2023 12:57:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=8F=8C=E9=87=8D=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=EF=BC=8C=E9=81=BF=E5=85=8D=E5=B9=B6=E5=8F=91=E4=B8=8B?= =?UTF-8?q?=E5=90=8C=E4=B8=80=E4=B8=AA=E6=AD=A3=E5=88=99=E8=A1=A8=E8=BE=BE?= =?UTF-8?q?=E5=BC=8F=E7=BC=96=E8=AF=91=E5=A4=9A=E6=AC=A1=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xyz/zhouxy/plusone/commons/util/RegexUtil.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/RegexUtil.java b/src/main/java/xyz/zhouxy/plusone/commons/util/RegexUtil.java index 5af00c3..9ecbd1a 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/RegexUtil.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/RegexUtil.java @@ -29,12 +29,16 @@ public class RegexUtil { public static Pattern getPattern(final String regex) { Objects.requireNonNull(regex); - if (PATTERN_CACHE.containsKey(regex)) { - return PATTERN_CACHE.get(regex); + if (!PATTERN_CACHE.containsKey(regex)) { + synchronized (RegexUtil.class) { + if (!PATTERN_CACHE.containsKey(regex)) { + Pattern pattern = Pattern.compile(regex); + PATTERN_CACHE.put(regex, pattern); + return pattern; + } + } } - Pattern pattern = Pattern.compile(regex); - PATTERN_CACHE.put(regex, pattern); - return pattern; + return PATTERN_CACHE.get(regex); } public static boolean matches(@Nullable CharSequence input, String regex) {