Skip to content

Instantly share code, notes, and snippets.

@whiter4bbit
Created October 3, 2014 14:25
Show Gist options
  • Save whiter4bbit/3b371693fc7c4fe9bf8a to your computer and use it in GitHub Desktop.
Save whiter4bbit/3b371693fc7c4fe9bf8a to your computer and use it in GitHub Desktop.
public final class InVar {
private static final InheritableThreadLocal<String> S = new InheritableThreadLocal<String>() {
@Override
protected String childValue(String parentValue) {
System.out.println("Child called");
return parentValue + "b";
}
};
public static void main(String[] args) throws Exception {
S.set("A");
final Runnable t2 = new Runnable() {
@Override
public void run() {
System.out.println("T2:" + S.get());
}
};
final Runnable t1 = new Runnable() {
@Override
public void run() {
System.out.println("T1:" + S.get());
S.set("C");
Thread t = new Thread(t2);
t.start();
try { t.join(); } catch (Exception e) { /**/ }
}
};
System.out.println("M:" + S.get());
Thread t = new Thread(t1);
t.start();
t.join();
}
}
/*
M:A
Child called
T1:Ab
Child called
T2:Cb
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment