public class A{
int value = 0;
final int NUMBER = 1000000;
public synchronized void haha(){
for (int i = 0; i < NUMBER; i++)
value ++;
}
class B extends TimerTask{
@Override
public void run(){
synchronized(A.this){
for (int i = 0; i < NUMBER; i++)
value ++;
}
}
}
public static void main(String args[]) throws InterruptedException {
A a = new A();
B b = a.new B();
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(2);
executor.scheduleWithFixedDelay(b, 1, 1, TimeUnit.SECONDS);
executor.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
a.haha();
}
}, 1, 1, TimeUnit.SECONDS);
Thread.sleep(1000 * 9 + 500);
executor.shutdownNow();
System.out.printf("value: %d\n", a.value);
}
}