Can someone please tell me why threadlocal.get() gives me null when i start a thread using ScheduledExecutorService ?
public class ThreadTest extends ParentClassThread
{
private static ScheduledFuture<?> periodicFuture;
private static ScheduledExecutorService ex;
public ThreadTest(){
ex = Executors.newSingleThreadScheduledExecutor();
periodicFuture = ex.schedule(this, 1, TimeUnit.SECONDS);
}
@Override
public void run() {
try {
System.out.println("Thread started");
for (int i = 0; i <= 100000; i++) {
System.out.println(i);
}
ThreadLocal local = new ThreadLocal();
System.out.println(local.get());
}catch(Exception e){
}finally {
ex.shutdown();
}
}
}
ThreadLocal<String> local = new ThreadLocal<String>();
local.set("String");
System.out.println(local.get());
You need to set something in ThreadLocalVariable and then retrieve it. Initially, ThreadLocal is empty.