java Concurrent Programming -- thread unsafe class

What are thread unsafe classes?

If an object of a class is accessed by multiple threads at the same time, if special synchronization or concurrent processing is not done, it is easy to show thread unsafe phenomena, such as throwing exceptions, logic processing errors, etc., which we call thread unsafe classes.

What are the common thread unsafe classes?

Here, I will focus on the SimpleDateFormat class and JodaTime

SimpleDateFormat is a thread unsafe class For example:

public class DateFormatExample1 {
   private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
   public static int clientTotal = 1000;//Total requests

   private  static void update() throws ParseException {
       simpleDateFormat.parse("20181008");
   }

   public static void main(String[] args)throws Exception {
       ExecutorService executorService = Executors.newCachedThreadPool(); //Define thread pool
       for (int i = 0; i < clientTotal; i++)
           executorService.execute(() -> {
               try {
                   update();
               } catch (ParseException e) {
                   e.printStackTrace();
               }
           });
   }
}

The way to solve this problem is to close the stack

public class DateFormatExample2 {

   public static int clientTotal = 1000;//Total requests

   private  static void update() throws ParseException {
       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
       simpleDateFormat.parse("20180208");
   }

   public static void main(String[] args)throws Exception {
       ExecutorService executorService = Executors.newCachedThreadPool(); //Define thread pool
       for (int i = 0; i < clientTotal; i++)
           executorService.execute(() -> {
               try {
                   update();
               } catch (ParseException e) {
                   e.printStackTrace();
               }
           });
   }
}

Jodatime provides a DateTimeFormatter class, which is thread safe and has more advantages in practical application. Therefore, it is recommended to use jodatime in actual development

You need to add maven dependency package first

<dependency>
   <groupId>joda-time</groupId>
   <artifactId>joda-time</artifactId>
   <version>2.9</version>
</dependency>
public class DateFormatExample2 {

   public static int clientTotal = 1000;//Total requests

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

   private  static void update() throws ParseException {
       dateTimeFormatter.parseDateTime("20181008");
   }

   public static void main(String[] args)throws Exception {
       ExecutorService executorService = Executors.newCachedThreadPool(); //Define thread pool
       for (int i = 0; i < clientTotal; i++)
           executorService.execute(() -> {
               try {
                   update();
               } catch (ParseException e) {
                   e.printStackTrace();
               }
           });
   }
}

Reference address:

Keywords: Maven

Added by nelietis on Wed, 18 Dec 2019 18:58:12 +0200