为什么向空Atomic类调用方法不会产生异常?

时间:2018-10-22 05:58:18

标签: java concurrency atomicity

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class _7_Synchronizing_Data_Access {
    private AtomicInteger count;

    private void incrementAndReport() {
        System.out.print(count.incrementAndGet() + "here"); //does not print
    }

    public static void main(String[] args) {
        ExecutorService service = null;
        try {
            service = Executors.newFixedThreadPool(20);
            _7_Synchronizing_Data_Access manager = new _7_Synchronizing_Data_Access();
            for (int i = 0; i < 10; i++)
                service.submit(() -> manager.incrementAndReport());
        } finally {
            if (service != null)
                service.shutdown();
        }
    }
}

运行该程序不会输出任何内容。甚至没有NullPointerException。如您所见,我没有实例化count。我希望它会引发错误。为什么会这样?

1 个答案:

答案 0 :(得分:7)

抛出NullPointerException异常,但要查看它们,您需要检查Future调用返回的service.submit实例:

按如下所示更改循环:

for (int i = 0; i < 10; i++) {
    Future f = service.submit(() -> manager.incrementAndReport());
    try {
        System.out.println (f.get ());
    }
    catch (ExecutionException exex) {
        System.out.println (exex);
    }
    catch (InterruptedException intEx) {
        System.out.println (intEx);
    }
}

将输出:

java.util.concurrent.ExecutionException: java.lang.NullPointerException
java.util.concurrent.ExecutionException: java.lang.NullPointerException
java.util.concurrent.ExecutionException: java.lang.NullPointerException
java.util.concurrent.ExecutionException: java.lang.NullPointerException
java.util.concurrent.ExecutionException: java.lang.NullPointerException
java.util.concurrent.ExecutionException: java.lang.NullPointerException
java.util.concurrent.ExecutionException: java.lang.NullPointerException
java.util.concurrent.ExecutionException: java.lang.NullPointerException
java.util.concurrent.ExecutionException: java.lang.NullPointerException
java.util.concurrent.ExecutionException: java.lang.NullPointerException

如果将System.out.println(count.incrementAndGet() + "here");语句用try-catch包围,您还将看到抛出异常:

private void incrementAndReport() {
    try {
        System.out.println(count.incrementAndGet() + "here"); //does not print
    }
    catch (Exception exc) {
        System.out.println (exc);
    }
}