apache / incubator-hugegraph-commons

(Archived Warning)Please visit "apache/hugegraph" repo instead
https://github.com/apache/hugegraph/tree/master/hugegraph-commons
Apache License 2.0
29 stars 45 forks source link

The SafeDateFormat is not thread safe #58

Closed Linary closed 3 years ago

Linary commented 3 years ago
Runnable runnable = () -> {
    int errorTimes = 0;
    for (int i = 0; i < 2; i++) {
        try {
            DateUtil.parse("0", "yyyy", "GTM+8");
        } catch (ParseException e) {
            e.printStackTrace();
            errorTimes++;
        }
    }
    System.out.println(Thread.currentThread() + " error times: " + errorTimes);
};
Thread thread1 = new Thread(runnable, "thread-1");
Thread thread2 = new Thread(runnable, "thread-2");
thread1.start();
thread2.start();
try {
    thread1.join();
    thread2.join();
} catch (InterruptedException e) {
    e.printStackTrace();
}

The result is:

Thread[thread-1,5,main] error times: 2
Thread[thread-2,5,main] error times: 0