java - Using Thread local with SDF and removing it after usage -


using simple date format not thread safe. in order avoid it, threadlocal used. used maintain state in each thread. should careful while using can lead memory leaks. sdf set on threalocal , later used read it. prevent memory leakage, should removed well. can please illustrate same example thread local needs removed after sdf usage.

below 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 client code calling , handling removal part of threadlocal well

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();        }  } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -