Я пытался запустить следующий код:
Код: Выделить всё
public class InheritableThreadLocalExample {
public static void main(String[] args) {
ThreadOne threadOne = new ThreadOne("user-one", "thread-one");
threadOne.start();
ThreadTwo threadTwo = new ThreadTwo("user-two", "thread-two");
threadTwo.start();
}
public static class ThreadOne extends Thread {
protected static final InheritableThreadLocal USERNAME = new InheritableThreadLocal();
public ThreadOne(String username, String threadName) {
super(threadName);
USERNAME.set(username);
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + ". Username: " + USERNAME.get());
}
}
public static class ThreadTwo extends ThreadOne {
public ThreadTwo(String username, String threadName) {
super(username, threadName);
}
}
}
Код: Выделить всё
thread-two. Username: user-one
thread-one. Username: null
Подробнее здесь: https://stackoverflow.com/questions/793 ... ted-thread
Мобильная версия