Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8629367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:47:12+00:00 2026-06-12T08:47:12+00:00

I want to show to a colleague that SimpleDateFormat is not thread-safe through a

  • 0

I want to show to a colleague that SimpleDateFormat is not thread-safe through a simple JUnit test. The following class fails to make my point (reusing SimpleDateFormat in a multi-threaded environment) and I don’t understand why. Can you spot what is preventing my use of SDF from throwing a runtime exception?

public class SimpleDateFormatThreadTest
{
    @Test
    public void test_SimpleDateFormat_MultiThreaded() throws ParseException{
        Date aDate = (new SimpleDateFormat("dd/MM/yyyy").parse("31/12/1999"));
        DataFormatter callable = new DataFormatter(aDate);

        ExecutorService executor = Executors.newFixedThreadPool(1000);
        Collection<DataFormatter> callables = Collections.nCopies(1000, callable);

        try{
            List<Future<String>> futures = executor.invokeAll(callables);
            for (Future f : futures){
                try{
                    assertEquals("31/12/1999", (String) f.get());
                }
                catch (ExecutionException e){
                    e.printStackTrace();
                }
            }
        }
        catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}

class DataFormatter implements Callable<String>{
    static SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

    Date date;

    DataFormatter(Date date){
        this.date = date;
    }

    @Override
    public String call() throws RuntimeException{
        try{
            return sdf.format(date);
        }
        catch (RuntimeException e){
            e.printStackTrace();
            return "EXCEPTION";
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:47:13+00:00Added an answer on June 12, 2026 at 8:47 am

    Lack of thread safety doesn’t necessarily mean that the code will throw an exception. This was explained in Andy Grove’s article, SimpleDateFormat and Thread Safety, which is no longer available online. In it, he showed SimpleDateFormat‘s lack of thread safety by showing that the output would not always be correct, given different inputs.

    When I run this code, I get the following output:

        java.lang.RuntimeException: date conversion failed after 3 iterations.
        Expected 14-Feb-2001 but got 01-Dec-2007
    

    Note that "01-Dec-2007" isn’t even one of the strings in the test data. It is actually a combination of the dates being processed by the other two threads!

    While the original article is no longer available online, the following code illustrates the issue. It was created based on articles that appeared to have been based on Andy Grove’s initial article.

    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.List;
    import java.util.Locale;
    
    public class SimpleDateFormatThreadSafety {
        private final SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
    
        public static void main(String[] args) {
            new SimpleDateFormatThreadSafety().dateTest(List.of("01-Jan-1999", "14-Feb-2001", "31-Dec-2007"));
        }
    
        public void dateTest(List<String> testData) {
            testData.stream()
                    .map(d -> new Thread(() -> repeatedlyParseAndFormat(d)))
                    .forEach(Thread::start);
        }
    
        private void repeatedlyParseAndFormat(String value) {
            for (int i = 0; i < 1000; i++) {
                Date d = tryParse(value);
                String formatted = dateFormat.format(d);
                if (!value.equals(formatted)) {
                    throw new RuntimeException("date conversion failed after " + i
                            + " iterations. Expected " + value + " but got " + formatted);
                }
            }
        }
    
        private Date tryParse(String value) {
            try {
                return dateFormat.parse(value);
            } catch (ParseException e) {
                throw new RuntimeException("parse failed");
            }
        }
    }
    

    Sometimes this conversion fails by returning the wrong date, and sometimes it fails with a NumberFormatException:

    java.lang.NumberFormatException: For input string: ".E2.31E2"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want show my tool tip after 5 second mouse enter. I try following
I want show a form as a dialog but problem is that the worker
I want show a trace(test) at the end of the yoyo How to know
I have two overlay images that I want show in camera view depending on
A simple scenario: I want to delete a pagelayout that is out-dated from our
I wrote a simple application and I want show delay of it with JProgressBar
I want show dialog after finish Thread. In thread I am changing TextView's Value
first I have a sortable container with class=viewport , then I want show each
I want show an information page after success db opartions. And after the info
I want show annotation view (above one of my pin) after load map. For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.