51,410
社区成员
发帖
与我相关
我的任务
分享
public class Test {
private Integer a = 0;
public static void main(String[] args){
Test test = new Test();
Runnable runnable = new Runnable() {
@Override
public void run() {
for (int i = 0;i<1000000;i++)
test.countA();
}
};
Thread thread = new Thread(runnable);
Thread threads = new Thread(runnable);
thread.start();
threads.start();
try{
thread.join();
threads.join();
}catch(Exception e){
e.printStackTrace();
}
System.out.println(test.a);
}
public void countA(){
synchronized (a){
a++;
}
}
}