JUC-ThreadLocal 落日映苍穹つ 2022-09-15 12:24 107阅读 0赞 * **ThreadLocal用完后要remove** **必须回收自定义的ThreadLocal变量,尤其在线程池场景下,线程经常会被复用,如果不清理自定义的ThreadLocal变量,可能会影响后续业务逻辑和造成内存泄漏等问题。尽量在代理中使用try-finally块进行回收。** ![watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA5Yqq5Yqb5aWL5paXR08_size_20_color_FFFFFF_t_70_g_se_x_16][] -------------------- /** * @author jl * @since 2021/10/11 19:51 * SimpleDateFormat线程安全问题示例 */ public class SimpleDateFormatDemo { public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); public static Date parse(String StringDateFormat) throws ParseException { return sdf.parse(StringDateFormat); } public static void main(String[] args) { for (int i = 0; i < 3; i++) { new Thread(()->{ try { System.out.println(SimpleDateFormatDemo.parse("2021-11-11 11:11:11")); } catch (ParseException e) { e.printStackTrace(); } }).start(); } } } 异常1 ![watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA5Yqq5Yqb5aWL5paXR08_size_20_color_FFFFFF_t_70_g_se_x_16 1][] 异常2 ![watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA5Yqq5Yqb5aWL5paXR08_size_20_color_FFFFFF_t_70_g_se_x_16 2][] 异常3 ![watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA5Yqq5Yqb5aWL5paXR08_size_20_color_FFFFFF_t_70_g_se_x_16 3][] 异常4 ![watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA5Yqq5Yqb5aWL5paXR08_size_9_color_FFFFFF_t_70_g_se_x_16][] ** 原因:** ![watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA5Yqq5Yqb5aWL5paXR08_size_20_color_FFFFFF_t_70_g_se_x_16 4][] ![watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA5Yqq5Yqb5aWL5paXR08_size_20_color_FFFFFF_t_70_g_se_x_16 5][] ** cal.clear();会清除别的线程设置的值** 解决: 1、加锁,影响性能,不推荐,有其他方式也能解决 2、**使用ThreadLocal<SimpleDateFormat>** public class SimpleDateFormatDemo { public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); public static Date parse(String StringDateFormat) throws ParseException { return sdf.parse(StringDateFormat); } public static final ThreadLocal<SimpleDateFormat> thread = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd")); public static Date parseByThreadLocal(String stringDateFormat) throws ParseException { return thread.get().parse(stringDateFormat); } public static void main(String[] args) { for (int i = 0; i < 30; i++) { new Thread(()->{ try { // System.out.println(SimpleDateFormatDemo.parse("2021-11-11 11:11:11")); System.out.println(SimpleDateFormatDemo.parseByThreadLocal("2021-11-11 11:11:11")); } catch (ParseException e) { e.printStackTrace(); } finally { thread.remove(); } }).start(); } } } [watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA5Yqq5Yqb5aWL5paXR08_size_20_color_FFFFFF_t_70_g_se_x_16]: /images/20220828/533300c8436541f49a7fd25f844fdcf7.png [watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA5Yqq5Yqb5aWL5paXR08_size_20_color_FFFFFF_t_70_g_se_x_16 1]: /images/20220828/ae4ccf8eef7e4df98fc7a6e599c53780.png [watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA5Yqq5Yqb5aWL5paXR08_size_20_color_FFFFFF_t_70_g_se_x_16 2]: /images/20220828/c8a3f36e37f646419f2c018964f76d21.png [watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA5Yqq5Yqb5aWL5paXR08_size_20_color_FFFFFF_t_70_g_se_x_16 3]: /images/20220828/5951c086e8ce4eac9c5c5cf45394d534.png [watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA5Yqq5Yqb5aWL5paXR08_size_9_color_FFFFFF_t_70_g_se_x_16]: /images/20220828/c02b8a2db9a04811a9b03f544aeb8d7d.png [watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA5Yqq5Yqb5aWL5paXR08_size_20_color_FFFFFF_t_70_g_se_x_16 4]: /images/20220828/03d057ae42274ddfa1feed60d5d66413.png [watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA5Yqq5Yqb5aWL5paXR08_size_20_color_FFFFFF_t_70_g_se_x_16 5]: /images/20220828/d411dc18b9b34337b5511cad5d881a64.png
还没有评论,来说两句吧...