Skip to content

Instantly share code, notes, and snippets.

@vega113
Last active October 6, 2017 12:45
Show Gist options
  • Save vega113/747c15df0859d975ca81 to your computer and use it in GitHub Desktop.
Save vega113/747c15df0859d975ca81 to your computer and use it in GitHub Desktop.
Example of Java 6/7 threaf safety issue with SimpleDateFormat.
package com.yuri.java8.time;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Created by yuri.zelikov on 1/11/2016.
*/
public class JodaDateThreadSafetyExample {
private static final DateTimeFormatter sdf = DateTimeFormat.forPattern("HH:mm:ss dd.MM.yyyy");
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(100);
while (true) {
executor.submit((Runnable) JodaDateThreadSafetyExample::workConcurrently);
}
}
public static void workConcurrently() {
try {
sdf.parseDateTime("19:30:55 03.05.2015");
System.out.print("OK ");
} catch (Throwable th) {
System.out.println(th.getClass() + " : " + th.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment