public class ThreadLocalExample {
static ThreadLocal<Integer> local = new ThreadLocal<Integer>(){
@Override
protected Integer initialValue(){
return 0;
}
};
public static void main(String[] args) {
Thread[] threads = new Thread[5];
for (int i = 0; i < 5 ;i++){
threads[i] = new Thread(() -> {
int num = local.get().intValue();
local.set(num += 5);
System.out.println(Thread.currentThread().getName() + " " + num);
});
}
for (int i = 0 ; i < 5 ; i++){
threads[i].start();
}
}
}
java多线程-ThreadLocal示例
最新推荐文章于 2024-11-16 22:34:09 发布