Can we use AtomicInteger as a local variable in a method and achieve thread safety?

Viewed 754
 public void tSafe(List<Foo> list, Properties status) {
    if(list == null) return;
    String key = "COUNT";
    AtomicInteger a = new AtomicInteger(Integer.valueOf(status.getProperty(key,"0")));
    list.parallelStream().filter(Foo::check).
            forEach(foo -> {status.setProperty(key, String.valueOf(a.incrementAndGet())); }
    );

}

private interface Foo {
    public boolean check();
}

Description:

In the above example, status is a shared properties and it contains a key with name COUNT. My aim is to increment count and put it back in properties to count the number of checks performed. Consider tSafe method is being called by multiple threads, Do I get the correct count at the end? Note that I've used AtomicInteger a as local variable.

2 Answers
Related