2

Using Simple Date format is not thread safe. In order to avoid it, ThreadLocal is used. It is used to maintain the state in each thread. We should be careful while using it as it can lead to memory leaks. The SDF is set on the threadlocal and later used to read from it. To prevent memory leakage, it should be removed as well. Can you please illustrate the same with an example where the thread local needs to be removed after the SDF usage.

Below is the code

public class ConcurrentDateFormatAccess {

 private ThreadLocal<DateFormat> df = new ThreadLocal<DateFormat> () {

  @Override
  public DateFormat get() {
   return super.get();
  }

  @Override
  protected DateFormat initialValue() {
   return new SimpleDateFormat("yyyy MM dd");
  }

  @Override
  public void remove() {
   super.remove();
  }

  @Override
  public void set(DateFormat value) {
   super.set(value);
  }

 };

 public Date convertStringToDate(String dateString) throws ParseException {
  return df.get().parse(dateString);
 }
}

Please help with the client code which would be calling this and also handling the removal part of threadLocal as well

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
Sachin
  • 39
  • 5
  • Maybe http://stackoverflow.com/questions/17968803/threadlocal-memory-leak –  Apr 29 '15 at 16:14
  • Here a [thread local cleaner](https://github.com/intercommit/basic-jsp-embed/blob/master/basic-jsp-embed/src/main/java/com/descartes/basicjsp/embed/ThreadLocalCleaner.java), but the real solution is to use [FastDateFormat](http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/time/FastDateFormat.html). – vanOekel Apr 29 '15 at 17:24
  • The reason for downvote would be helpful. – Amit Parashar Apr 29 '15 at 19:33
  • Can I know the reason for down vote ?? – Sachin Apr 30 '15 at 06:17

1 Answers1

0
package com.so;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ConcurrentDateFormatAccess {



    private static ThreadLocal<DateFormat> df = new ThreadLocal<DateFormat> () {

      @Override
      public DateFormat get() {
       return super.get();
      }

      @Override
      protected DateFormat initialValue() {
       return new SimpleDateFormat("yyyy MM dd");
      }

      @Override
      public void remove() {
       super.remove();
      }

      @Override
      public void set(DateFormat value) {
       super.set(value);
      }

     };

     public static  Date convertStringToDate(String dateString) throws ParseException {
      return df.get().parse(dateString);
     }

     public static void clean() throws ParseException {
           df.remove();
    }


    }

Your Client :

package com.so;

import java.text.ParseException;

public class SOQ {

    public static void main(String[] args) {

        Thread thread1 = new Thread() {
            @Override
            public void run() {
                try {
                    System.out.println(ConcurrentDateFormatAccess.convertStringToDate("2015 05 01"));
                    ConcurrentDateFormatAccess.clean();
                } catch (ParseException e) {// TODO Auto-generated catch block
                    e.printStackTrace();
                }

                super.run();
            }
        };
        Thread thread2 = new Thread() {
            @Override
            public void run() {
                try {
                    System.out.println(ConcurrentDateFormatAccess.convertStringToDate("2015 05 02"));
                    ConcurrentDateFormatAccess.clean();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                super.run();
            }
        };
        thread1.start();
        thread2.start();



    }

}
Amit Parashar
  • 1,447
  • 12
  • 15
  • Thanks but what is the purpose of remove method ?? How it cleans the thread local – Sachin Apr 30 '15 at 06:20
  • As you asked ,After the call to clean() method, the thread will not have SimpleDateFormat() object . You can refer the docs : https://docs.oracle.com/javase/7/docs/api/java/lang/ThreadLocal.html#remove() – Amit Parashar Apr 30 '15 at 06:38
  • Maybe it is more safe to call clean() in a finally-clause?! – Meno Hochschild Apr 30 '15 at 09:44