Hello. It's Kecho. ** Do you guys use SimpleDateFormat and stream? ** ** It's a niche. I love stream. This time we will verify that SimpleDateFormat is thread unsafe.
First, prepare a Date type object.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
Calendar day1 = Calendar.getInstance();
day1.set(2020, 0, 31, 0, 0, 0);
Calendar day2 = Calendar.getInstance();
day2.set(1995, 3, 6, 0, 0, 0);
Let's put it in the list.
for (int i = 0; i < 10; i++) {
//Fill the list with 20 Date objects
list.add(day1.getTime());
list.add(day2.getTime());
}
First, process without executing in parallel.
list.stream().map(date -> sdf.format(date)).forEach(System.out::println);
//Convert each element to String in turn and output
The whole picture is below.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
Calendar day1 = Calendar.getInstance();
day1.set(2020, 0, 31, 0, 0, 0);
Calendar day2 = Calendar.getInstance();
day2.set(1995, 3, 6, 0, 0, 0);
List<Date> list = new ArrayList<>(); //Creating a list
for (int i = 0; i < 10; i++) {
//Fill the list with Date objects
list.add(day1.getTime());
list.add(day2.getTime());
}
list.stream().map(date -> sdf.format(date)).forEach(System.out::println);
2020/01/31 12:00:00
1995/04/06 12:00:00
2020/01/31 12:00:00
1995/04/06 12:00:00
2020/01/31 12:00:00
1995/04/06 12:00:00
2020/01/31 12:00:00
1995/04/06 12:00:00
2020/01/31 12:00:00
1995/04/06 12:00:00
2020/01/31 12:00:00
1995/04/06 12:00:00
2020/01/31 12:00:00
1995/04/06 12:00:00
2020/01/31 12:00:00
1995/04/06 12:00:00
2020/01/31 12:00:00
1995/04/06 12:00:00
2020/01/31 12:00:00
1995/04/06 12:00:00
There seems to be no problem.
stream will do parallel processing just by adding parallel () to the intermediate operation.
list.stream().map(date -> sdf.format(date)).forEach(System.out::println);
Let's do it!
2020/01/31 12:00:00
1995/01/31 12:00:00 //what's this
1995/04/06 12:00:00
2020/01/31 12:00:00
2020/01/31 12:00:00
2020/01/31 12:00:00
2020/01/31 12:00:00
1995/04/06 12:00:00
1995/04/06 12:00:00
2020/01/31 12:00:00
1995/04/06 12:00:00
2020/01/31 12:00:00
1995/04/06 12:00:00
2020/01/31 12:00:00
1995/04/06 12:00:00
1995/04/06 12:00:00
2020/01/31 12:00:00
1995/04/06 12:00:00
2020/01/31 12:00:00
1995/04/31 12:00:00 //what's this
Some unexpected dates were output. I was able to confirm that it is thread unsafe.
You may create a SimpleDateFormat object in each thread. Basically, it is better to use the Date And Time API implemented from Java 8.
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd hh:mm:ss");
LocalDateTime today = LocalDateTime.now();
LocalDateTime birthDay = LocalDateTime.of(1995, 4, 6, 12, 0, 0);
List<LocalDateTime> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
//Fill the list with Date objects
list.add(today);
list.add(birthDay);
}
list.stream().parallel().map(date -> dtf.format(date)).forEach(System.out::println);
2020/01/31 09:33:20
1995/04/06 12:00:00
1995/04/06 12:00:00
2020/01/31 09:33:20
1995/04/06 12:00:00
2020/01/31 09:33:20
2020/01/31 09:33:20
2020/01/31 09:33:20
2020/01/31 09:33:20
2020/01/31 09:33:20
2020/01/31 09:33:20
1995/04/06 12:00:00
1995/04/06 12:00:00
1995/04/06 12:00:00
1995/04/06 12:00:00
1995/04/06 12:00:00
2020/01/31 09:33:20
2020/01/31 09:33:20
1995/04/06 12:00:00
1995/04/06 12:00:00
Sounds okay.
Use Date And Time API for Java 8 and above
Recommended Posts