arthas icon indicating copy to clipboard operation
arthas copied to clipboard

WIP: watch命令支持CompletableFuture

Open robberphex opened this issue 4 years ago • 3 comments

实现了watch命令的-a参数,以支持watch异步方法。

[arthas@6906]$ watch Main asyncAction returnObj
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 57 ms.
ts=2020-04-04 10:37:24; [cost=3.6561ms] result=@CompletableFuture[
    result=null,
    stack=null,
    NIL=@AltResult[java.util.concurrent.CompletableFuture$AltResult@4f8e5cde],
    USE_COMMON_POOL=@Boolean[true],
    ASYNC_POOL=@ForkJoinPool[java.util.concurrent.ForkJoinPool@3b764bce[Running, parallelism = 7, size = 1, active = 1, running = 0, steals = 3, tasks
 = 0, submissions = 0]],
    SYNC=@Integer[0],
    ASYNC=@Integer[1],
    NESTED=@Integer[-1],
    RESULT=@FieldInstanceReadWrite[java.lang.invoke.VarHandleObjects$FieldInstanceReadWrite@484b61fc],
    STACK=@FieldInstanceReadWrite[java.lang.invoke.VarHandleObjects$FieldInstanceReadWrite@45fe3ee3],
    NEXT=@FieldInstanceReadWrite[java.lang.invoke.VarHandleObjects$FieldInstanceReadWrite@4cdf35a9],
]
[arthas@6906]$ watch Main asyncAction returnObj -a
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 23 ms.
ts=2020-04-04 10:37:34; [cost=4.80702489339E8ms] result=@Integer[4]
ts=2020-04-04 10:37:39; [cost=4.807075062602E8ms] result=@Integer[5]
Main.java代码

import java.util.concurrent.CompletableFuture;

public class Main {
    private static int cnt = 0;

    private static CompletableFuture<Integer> asyncAction() {
        return CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return Main.cnt++;
        });
    }

    public static void main(String[] args) {
        while (true) {
            asyncAction().thenAccept((val) -> {
                System.out.println(val);
            }).join();
        }
    }
}

robberphex avatar Apr 04 '20 02:04 robberphex

Arthas 现在还是需要保持对 Java 6 的兼容性,看起来这个 PR 暂时不会被 merge 了。

XhinLiang avatar May 02 '20 14:05 XhinLiang

@RobberPhex R 异步方法不能使用ThreadLocal传递值,方法cost的计算方式要改变。 另外要考虑兼容jdk 1.6下运行不报错。

kylixs avatar May 08 '20 12:05 kylixs

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Jun 12 '20 07:06 CLAassistant