public class CodeWithLocal implements Runnable {
public ThreadLocal local = new ThreadLocal();
public Object shared;
public void run() {
String name = Thread.currentThread().getName();
local.set(new Long(Math.round(Math.random()*100)));
System.out.println(name + ": locale: " + local.get());
shared = new Long(Math.round(Math.random()*100));
System.out.println(name + ": partagée: " + shared);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println(name + ": après attente, locale: " + local.get());
System.out.println(name + ": après attente, partagée: " + shared);
}
}