public class Test {
public int id;
public String name;
private static long count;
@SuppressWarnings("rawtypes")
private final static ThreadLocal tl=new ThreadLocal();
@SuppressWarnings("unchecked")
public Test() {
super();
count++;
tl.set(count);
}
public long getCount(){
return (Long)tl.get();
}
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
Test t=new Test();
System.out.println(t.getCount());
}
}
}
输出
1
2
3