public class TestA {
@SuppressWarnings("unchecked")
public static <T> void main(String...args) throws InterruptedException {
Thread.currentThread().setName("--Parent--");
final InheritableThreadLocal<T> itl = new InheritableThreadLocal<T>() {
@Override
protected T childValue(T i)
{
System.out.println(Thread.currentThread().getName() + "\r\n" + i );
return i;
}
};
itl.set((T) new Integer(33));
Runnable r = new Runnable() {
@Override
public void run() {
Thread.currentThread().setName("--local--");
System.out.println(Thread.currentThread().getName() + "\r\n" + itl.get());
}
};
new Thread(r).start();
System.out.println(itl.get());
}
}
那么如何在这里指定类型。由于上述代码中没有指定类型,所以默认的对象类型在图片中。我想让本地内部类“InheritableThreadLocal”通用。