4

I have written some code that I believe is likely to throw an exception. But an IDEA Ide #IC-231.8109.175 surprisingly show me warning Condition 'id != Thread.currentThread().threadId()' is always 'false'. I am doubtful about the accuracy of this warning and interested why it really always evaluates to false.

import java.util.stream.IntStream;

public class Main {

    public static void main(String[] args) {
        final long id = Thread.currentThread().threadId();
        var g = IntStream.range(0, 100_000_000).boxed()
                .parallel()
                .map(k -> {
                    if (id != Thread.currentThread().threadId()) {
                        throw new RuntimeException();
                    }
                    return k + 1;
                }).findAny();

        System.out.println(g);
    }
}
osseum
  • 187
  • 14
  • 2
    It's not throwing the exception because there's no need for parallelism when you're using `findAny()` with no filter. Changing it to `findFirst()` throws an exception but doesn't remove the warning, strangely. – shmosel Aug 16 '23 at 17:21
  • @shmosel Oops, my mistake, misread the code. – VGR Aug 16 '23 at 19:38
  • @Holger Why is there no need to evaluate the mapping function? – shmosel Aug 21 '23 at 17:41
  • @shmosel my bad, I mixed it up with the logic of `isPresent()` – Holger Aug 22 '23 at 07:34

0 Answers0