⟩ What is thread Local variable?
If a variable is declared as threadLocal then all the thread which accesses that variable will maintain its own copy of variable and would not use the copy of any other thread.
For example let consider the scenario of giving JDBC connection to each thread.
public class ThreadLocal {
public Object get();
public void set(Object myValue);
public Object FirstValue();
}
Implementation of ThreadLocal
public class ConnectionDispenser {
private static class ThreadLocalConnection extends ThreadLocal {
public Object FirstValue () {
return DriverManager.getConnection(ConfigurationSingleton.getDbUrl());
}
}
private static ThreadLocalConnection con = new ThreadLocalConnection();
public static Connection MyConnection() {
return (Connection) con.get();
}
}