Java Secret: InheritableThreadLocal
Overview
If you follow functional programming methodologies, every dependency for a function is passed to it including all dependencies of the functions its calls etc. This model can work very well because it makes it clear all the values which a piece of code depends on.However, it can mean passing dependencies through code which shouldn't need to know about these values. (Or in fact cannot be changed to include them)
Using a ThreadLocal
A way to avoid passing values over level of calls which don't know anything about them is to use a ThreadLocal. This allows you to set a value which is available at any point in the call stack in a thread safe way. Its like passing a ball into the air you want to run and catch later.static final ThreadLocalprintsuserName = new ThreadLocal (); public static void main(String... args) { userName.set("Jane"); Runnable run = new Runnable() { @Override public void run() { String threadName = Thread.currentThread().getName(); String name = userName.get(); System.out.println(threadName +": Welcome "+name); } }; run.run(); }
main: Welcome Jane
As you can see I can set a value I don't want or cannot pass as an argument.
InheritableThreadLocal
However, say I want to be able to sporn threads and still keep the value. In this case you can use an InheritableThreadLocal which passes the value to child threads.static final ThreadLocalprintsuserName = new InheritableThreadLocal (); public static void main(String... args) { userName.set("Jane"); Runnable run = new Runnable() { @Override public void run() { String threadName = Thread.currentThread().getName(); String name = userName.get(); System.out.println(threadName +": Welcome "+name); } }; new Thread(run).start(); }
thread-0: Welcome Jane
In the first sample using ThreadLocal, In the code it run as:
ReplyDeleterun.run();
It does not seems to be run in a new thread so the context was run at same thread.
If use new Thread(run).start(), the context run in different thread, the out put should be null.
This is correct. If you use ThreadLocal and run in a different thread it would be null which is why it is not run in a different thread.
Deleteawful and completely wrong example
ReplyDeleteCan you clarify what you mean? Are you saying that the code doesn't do what the post says?
Delete