Missing Thread in when using lambda callable

Questions about YourKit Java Profiler
Post Reply
BigMichi
Posts: 1
Joined: Sat Jul 21, 2018 7:22 am

Missing Thread in when using lambda callable

Post by BigMichi »

currently i'm trying to find out why i don't see the created thread from the executor servcie in the profiler when i write the Callable as a lambda expression. i'm running the latest java profiler 2018.04.b80 with jdk 1.8_712

given this pice of code

Code: Select all

public class Main {

    private AtomicInteger x = new AtomicInteger();

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        Main p = new Main();
        ExecutorService executor = Executors.newFixedThreadPool(2);
        Callable<Integer> c = () -> p.x.accumulateAndGet(3, (a, b) -> a + b);
        executor.submit(c).get();
        executor.submit(c).get();
        System.out.println(p.x.get());
        executor.shutdown();
    }
}
results in
Image

but writing itin this way

Code: Select all

public class Main {

    private AtomicInteger x = new AtomicInteger();

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        Main p = new Main();
        ExecutorService executor = Executors.newFixedThreadPool(2);
        Callable<Integer> c = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                return p.x.accumulateAndGet(3, (a, b) -> a + b);
            }
        };
        executor.submit(c).get();
        executor.submit(c).get();
        System.out.println(p.x.get());
        executor.shutdown();
    }
}
results in
Image

shouldn't it not be the same? or am i missing something?
Anton Katilin
Posts: 6172
Joined: Wed Aug 11, 2004 8:37 am

Re: Missing Thread in when using lambda callable

Post by Anton Katilin »

Hi,

The lambda code in your example completes almost immediately:

Code: Select all

Callable<Integer> c = () -> p.x.accumulateAndGet(3, (a, b) -> a + b);
Sampling rate is 20 ms, it's pretty improbable that such short call will be registered with sampling.

Adding sleep() to the lambda body makes the calls clearly visible:

Code: Select all

            Callable<Integer> c = () -> p.x.accumulateAndGet(3, (a, b) -> {
              try {
                Thread.sleep(1000);
              }
              catch (InterruptedException e) {
              }
              return a + b;
            });
I also recommend to switch to "Call tree - by thread" mode to explicitly see what happens in the main thread and what in the pool threads.

Best regards,
Anton
Post Reply