62,621
社区成员
发帖
与我相关
我的任务
分享public class ThreadLocalTest {
private static ThreadLocal<Integer> value = new ThreadLocal<Integer>(){
@Override
protected Integer initialValue() {
return 1;
}
};
private static int testnum = 1;
static class Mythrad implements Runnable{
public void run() {
int num = value.get();
int tem = (int)(Math.random()*10);
System.out.println(Thread.currentThread().getName()+"---random num==="+tem);//1
testnum += tem;
//2
value.set(num+tem);
System.out.println(Thread.currentThread().getName()+"----"+tem+"-------"+value.get()+">>><<<<"+testnum);
}
}
public static void main(String[] args) {
Thread t1 = new Thread(new Mythrad());
Thread t2 = new Thread(new Mythrad());
t1.start();
t2.start();
}
}