java - ThreadLocal variable for SimpleDateFormat (OR) new SimpleDateFormat - Correct? -
i come across this article , got confused
in our project in few places using threadlocal constructing threadlocal variable
in few other places constructing new object simpledateformat inside private local method.
example,
private date getconverteddate(string date){ simpledateformat sdf = new simpledateformat(); --> thread safe or not }
could clarify in detail?
thanks.
variables created inside method - live on stack, are not shared across threads. each thread has it's own stack. declaring inside method, working it, forgetting it, make thread safe.
after comments here snippet:
public class deleteme { public static void main(string[] args) throws exception { deleteme me = new deleteme(); me.go(); } public void go() throws exception { final mutable mutable = new mutable(); new thread(new runnable() { @override public void run() { mutable.sett(57); } }).start(); /** main thread waits bit sure custom thread start */ thread.sleep(2000); system.out.println(mutable.gett()); } private class mutable{ public int t = 0; public int gett() { return t; } public void sett(int t) { this.t = t; } } }
Comments
Post a Comment