SimpleDateFormat 线程安全问题的六种解决方案

3552 篇文章 114 订阅

解决 SimpleDateFormat 类在高并发场景下的线程安全问题可以有多种方式,这里,就列举几个常用的方式供参考,大家也可以在评论区给出更多的解决方案。

1、局部变量法

最简单的一种方式就是将 SimpleDateFormat 类对象定义成局部变量,如下所示的代码,将 SimpleDateFormat 类对象定义在 parse(String) 方法的上面,即可解决问题。

package com.nowjava.date.test;
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 binghe * @version 1.0.0 * @description 局部变量法解决SimpleDateFormat类的线程安全问题 */public class SimpleDateFormatTest02 {
  //执行总次数  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("所有线程格式化日期成功");  }}

此时运行修改后的程序,输出结果如下所示。

所有线程格式化日期成功

当然,这种方式在高并发下会创建大量的 SimpleDateFormat 类对象,影响程序的性能,所以,这种方式在实际生产环境不太被推荐

2、synchronized 锁方式

将 SimpleDateFormat 类对象定义成全局静态变量,此时所有线程共享 SimpleDateFormat 类对象,此时在调用格式化时间的方法时,对 SimpleDateFormat 对象进行同步即可,代码如下所示。

package com.nowjava.date.test;
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 binghe * @version 1.0.0 * @description 通过Synchronized锁解决SimpleDateFormat类的线程安全问题 */public class SimpleDateFormatTest03 {
  //执行总次数  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 {            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("所有线程格式化日期成功");  }}

此时,解决问题的关键代码如下所示。

synchronized (simpleDateFormat){    simpleDateFormat.parse("2020-01-01");}

运行程序,输出结果如下所示。

所有线程格式化日期成功

需要注意的是,虽然这种方式能够解决 SimpleDateFormat 类的线程安全问题,但是由于在程序的执行过程中,为 SimpleDateFormat 类对象加上了 synchronized 锁,导致同一时刻只能有一个线程执行 parse(String) 方法。此时,会影响程序的执行性能,在要求高并发的生产环境下,此种方式也是不太推荐使用的。

3、Lock 锁方式

Lock 锁方式与 synchronized 锁方式实现原理相同,都是在高并发下通过 JVM 的锁机制来保证程序的线程安全。通过 Lock 锁方式解决问题的代码如下所示。

package com.nowjava.date.test;
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;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;
/** * @author binghe * @version 1.0.0 * @description 通过Lock锁解决SimpleDateFormat类的线程安全问题 */public class SimpleDateFormatTest04 {
  //执行总次数  private static final int EXECUTE_COUNT = 1000;  //同时运行的线程数量  private static final int THREAD_COUNT = 20;  //SimpleDateFormat对象  private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat(    "yyyy-MM-dd"  );  //Lock对象  private static Lock lock = new ReentrantLock();
  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("所有线程格式化日期成功");  }}

通过代码可以得知,首先,定义了一个 Lock 类型的全局静态变量作为加锁和释放锁的句柄。然后在 simpleDateFormat。parse(String) 代码之前通过 lock。lock() 加锁。这里需要注意的一点是:为防止程序抛出异常而导致锁不能被释放,一定要将释放锁的操作放到 finally 代码块中,如下所示。

finally {  lock.unlock();}

运行程序,输出结果如下所示。

所有线程格式化日期成功

此种方式同样会影响高并发场景下的性能,不太建议在高并发的生产环境使用。

4、ThreadLocal 方式

使用 ThreadLocal 存储每个线程拥有的 SimpleDateFormat 对象的副本,能够有效的避免多线程造成的线程安全问题,使用 ThreadLocal 解决线程安全问题的代码如下所示。

package com.nowjava.date.test;
import java.text.DateFormat;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 binghe * @version 1.0.0 * @description 通过ThreadLocal解决SimpleDateFormat类的线程安全问题 */public class SimpleDateFormatTest05 {
  //执行总次数  private static final int EXECUTE_COUNT = 1000;  //同时运行的线程数量  private static final int THREAD_COUNT = 20;  private static ThreadLocal<DateFormat> threadLocal = new ThreadLocal<DateFormat>() {    @Override    protected DateFormat initialValue() {      return 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 {            threadLocal.get().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("所有线程格式化日期成功");  }}
通过代码可以得知,将每个线程使用的 SimpleDateFormat 副本保存在 ThreadLocal 中,各个线程在使用时互不干扰,从而解决了线程安全问题。

运行程序,输出结果如下所示。

所有线程格式化日期成功

此种方式运行效率比较高,推荐在高并发业务场景的生产环境使用。

另外,使用 ThreadLocal 也可以写成如下形式的代码,效果是一样的。

5、DateTimeFormatter 方式

DateTimeFormatter 是 Java8 提供的新的日期时间 API 中的类,DateTimeFormatter 类是线程安全的,可以在高并发场景下直接使用 DateTimeFormatter 类来处理日期的格式化操作。代码如下所示。

package com.nowjava.date.test;
import java.time.LocalDate;import java.time.format.DateTimeFormatter;import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Semaphore;
/** * @author binghe * @version 1.0.0 * @description 通过DateTimeFormatter类解决线程安全问题 */public class SimpleDateFormatTest07 {
  //执行总次数  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("所有线程格式化日期成功");  }}

可以看到,DateTimeFormatter 类是线程安全的,可以在高并发场景下直接使用 DateTimeFormatter 类来处理日期的格式化操作。

运行程序,输出结果如下所示。

所有线程格式化日期成功

使用 DateTimeFormatter 类来处理日期的格式化操作运行效率比较高,推荐在高并发业务场景的生产环境使用。

6、joda-time 方式

joda-time 是第三方处理日期时间格式化的类库,是线程安全的。如果使用 joda-time 来处理日期和时间的格式化,则需要引入第三方类库。这里,以 Maven 为例,如下所示引入 joda-time 库。

<dependency>    <groupId>joda-time</groupId>    <artifactId>joda-time</artifactId>    <version>2.9.9</version></dependency>

引入 joda-time 库后,实现的程序代码如下所示。

package com.nowjava.date.test;
import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Semaphore;import org.joda.time.DateTime;import org.joda.time.format.DateTimeFormat;import org.joda.time.format.DateTimeFormatter;
/** * @author binghe * @version 1.0.0 * @description 通过DateTimeFormatter类解决线程安全问题 */public class SimpleDateFormatTest08 {
  //执行总次数  private static final int EXECUTE_COUNT = 1000;  //同时运行的线程数量  private static final int THREAD_COUNT = 20;  private static DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(    "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 {            DateTime.parse("2020-01-01", dateTimeFormatter).toDate();          } 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("所有线程格式化日期成功");  }}

这里,需要注意的是:DateTime 类是 org。joda。time 包下的类,DateTimeFormat 类和 DateTimeFormatter 类都是 org。joda。time。format 包下的类,如下所示。

import org.joda.time.DateTime;import org.joda.time.format.DateTimeFormat;import org.joda.time.format.DateTimeFormatter;

运行程序,输出结果如下所示。

所有线程格式化日期成功

使用 joda-time 库来处理日期的格式化操作运行效率比较高,推荐在高并发业务场景的生产环境使用。

总结

综上所示:在解决解决 SimpleDateFormat 类的线程安全问题的几种方案中,局部变量法由于线程每次执行格式化时间时,都会创建 SimpleDateFormat 类的对象,这会导致创建大量的 SimpleDateFormat 对象,浪费运行空间和消耗服务器的性能,因为 JVM 创建和销毁对象是要耗费性能的。所以,不推荐在高并发要求的生产环境使用。

synchronized 锁方式和 Lock 锁方式在处理问题的本质上是一致的,通过加锁的方式,使同一时刻只能有一个线程执行格式化日期和时间的操作。这种方式虽然减少了 SimpleDateFormat 对象的创建,但是由于同步锁的存在,导致性能下降,所以,不推荐在高并发要求的生产环境使用。

ThreadLocal 通过保存各个线程的 SimpleDateFormat 类对象的副本,使每个线程在运行时,各自使用自身绑定的 SimpleDateFormat 对象,互不干扰,执行性能比较高,推荐在高并发的生产环境使用。

DateTimeFormatter 是 Java 8 中提供的处理日期和时间的类,DateTimeFormatter 类本身就是线程安全的,经压测,DateTimeFormatter 类处理日期和时间的性能效果还不错。所以,推荐在高并发场景下的生产环境使用。

joda-time 是第三方处理日期和时间的类库,线程安全,性能经过高并发的考验,推荐在高并发场景下的生产环境使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值