Created
October 3, 2014 14:25
-
-
Save whiter4bbit/3b371693fc7c4fe9bf8a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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