Fully featured low overhead profiler for Java EE and Java SE platforms.
Performance monitoring and profiling of Jenkins, TeamCity, Gradle, Maven, Ant, JUnit and TestNG.
Easy to use performance and memory profiler for .NET framework.

Missing Thread in when using lambda callable

Moderators: Vladimir Kondratyev, Anton Katilin

Missing Thread in when using lambda callable

Postby BigMichi » Sat Jul 21, 2018 7:32 am

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?
BigMichi
 
Posts: 1
Joined: Sat Jul 21, 2018 7:22 am
Location: Munich

Re: Missing Thread in when using lambda callable

Postby Anton Katilin » Mon Jul 23, 2018 8:17 am

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
Anton Katilin
 
Posts: 5719
Joined: Wed Aug 11, 2004 8:37 am


Return to Java Profiler

Who is online

Users browsing this forum: No registered users and 12 guests