public class Calc {
public int k = 0;
public synchronized void add(){
k++;
System.out.println(k);
}
public static void main(String[] args) {
final Calc a = new Calc();
for (int i = 0; i < 5; i++) {
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
a.add();
}
}
}).start();
}
}
}
【不管常见几个线程,此时this等于a】 |