ADDITIONAL SYSTEM INFORMATION :
java.vm.vendor=Oracle Corporation
java.vm.name=Java HotSpot(TM) 64-Bit Server VM
java.version=1.8.0_221
java.specification.version=1.8
java.runtime.version=1.8.0_221-b11
user.language=en
user.country=GB
user.variant=
Default locale=en_GB
os.name=Mac OS X
os.arch=x86_64
os.version=10.16
A DESCRIPTION OF THE PROBLEM :
As a result of https://bugs.openjdk.java.net/browse/JDK-8025636 lambdas are hidden in stack traces.
Sadly, that is true only when stack trace is taken from the same thread.
When Thread.getStackTrace() is taken from a different thread than the invoked object, the stack trace still contains lambdas.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the attached application.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
----------From different thread----------
java.lang.Thread.sleep(Native Method)
HiddenLambda.bar(HiddenLambda.java:36)
HiddenLambda.lambda$null$0(HiddenLambda.java:10)
HiddenLambda.foo(HiddenLambda.java:31)
HiddenLambda.lambda$main$1(HiddenLambda.java:10)
java.lang.Thread.run(Thread.java:748)
----------From the same thread----------
HiddenLambda.bar(HiddenLambda.java:38)
HiddenLambda.lambda$null$0(HiddenLambda.java:10)
HiddenLambda.foo(HiddenLambda.java:31)
HiddenLambda.lambda$main$1(HiddenLambda.java:10)
java.lang.Thread.run(Thread.java:748)
ACTUAL -
----------From different thread----------
java.lang.Thread.sleep(Native Method)
HiddenLambda.bar(HiddenLambda.java:36)
HiddenLambda.lambda$null$0(HiddenLambda.java:10)
HiddenLambda$$Lambda$2/277640063.run(Unknown Source)
HiddenLambda.foo(HiddenLambda.java:31)
HiddenLambda.lambda$main$1(HiddenLambda.java:10)
HiddenLambda$$Lambda$1/531885035.run(Unknown Source)
java.lang.Thread.run(Thread.java:748)
----------From the same thread----------
HiddenLambda.bar(HiddenLambda.java:38)
HiddenLambda.lambda$null$0(HiddenLambda.java:10)
HiddenLambda.foo(HiddenLambda.java:31)
HiddenLambda.lambda$main$1(HiddenLambda.java:10)
java.lang.Thread.run(Thread.java:748)
---------- BEGIN SOURCE ----------
import java.util.Arrays;
import java.util.List;
import java.util.Set;
public class HiddenLambda {
public static void main(String[] args) {
Thread myThread = new Thread(()->
{
foo(() -> bar());
});
myThread.start();
Set<Thread> threads = Thread.getAllStackTraces().keySet();
for (Thread thread : threads) {
if (myThread == thread){
System.out.println("----------From a different thread----------");
List<StackTraceElement> stackTraceElements = Arrays.asList(thread.getStackTrace());
for (StackTraceElement stackTraceElement : stackTraceElements) {
System.out.println(stackTraceElement);
}
}
}
try {
myThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
static void foo(Runnable lambda) {
lambda.run();
}
static void bar() {
try {
Thread.sleep(1000);
System.out.println("----------From the same thread----------");
List<StackTraceElement> stackTraceElements = Arrays.asList((new Exception()).getStackTrace());
for (StackTraceElement stackTraceElement : stackTraceElements) {
System.out.println(stackTraceElement);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
I found no workaround to hide the lambdas, but for consistency between snapshots you can disable hiding lambdas by adding VM options:
-XX:+UnlockDiagnosticVMOptions -XX:+ShowHiddenFrames
FREQUENCY : always