Showing posts with label Executor. Show all posts
Showing posts with label Executor. Show all posts

April 01, 2016

Part 7: Java Thread Interview Questions & Answers (Thread Pool, Executor)



What is ThreadLocal variable in Java?

ThreadLocal is a special class for which each thread accessing the variable would have a separate instance of it. This was introduced on JDK 1.2 but it was later generified in JDK 1.4 to introduce type safety on ThreadLocal variable.

The ThreadLocal variable becomes eligible for Garbage collection after the thread finished or died, normally or due to any Exception (given those ThreadLocal variable doesn't have any other live references).

ThreadLocal is a generic class and can be applied to any type.

ThreadLocal variables in Java are generally private static fields in Classes and maintain its state inside Thread. e.g :

private static final ThreadLocal < SimpleDateFormat > dateformatter = new ThreadLocal < SimpleDateFormat > ();