Environment: jdk1.8
public static AtomicInteger num = new AtomicInteger(0);
public static void main(String[] args) throws Throwable {
Runnable runnable = () -> {
for (int i = 0; i < 1000000000; i++) {
num.getAndAdd(1);
}
};
Thread t1 = new Thread(runnable);
Thread t2 = new Thread(runnable);
t1.start();
t2.start();
System.out.println("before sleep");
Thread.sleep(1000);
System.out.println("after sleep");
System.out.println(num);
}
I saw this question :The main thread exceeds the set sleep time (I want to add comments to this question, but I don' have enough reputation, so I create this new question. If this is a discouraged operation, I'm sorry for this.)
But I don't know why getAndAddInt method with no safepoint checks inside?
Doesn't the getAndAddInt method also contain native methods?