0

主要课程:

package com.xxx.yyy;

public class Hello{
    public static void main(String[] args){
        A a = new A();
        while(true){
            try {
                a.execute(1000);
                Thread.sleep(1000);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

A类:

package com.xxx.yyy;

public class A{
    public void execute(int sleepTime) throws Exception {
        System.out.println("sleep time is "+sleepTime); 
    }
}

btrace 脚本:

import static com.sun.btrace.BTraceUtils.println;  
import static com.sun.btrace.BTraceUtils.str;  
import static com.sun.btrace.BTraceUtils.strcat;  
import static com.sun.btrace.BTraceUtils.timeMillis;  

import com.sun.btrace.annotations.BTrace;  
import com.sun.btrace.annotations.Kind;  
import com.sun.btrace.annotations.Location;  
import com.sun.btrace.annotations.OnMethod;  
import com.sun.btrace.annotations.ProbeClassName;  
import com.sun.btrace.annotations.ProbeMethodName;  
import com.sun.btrace.annotations.TLS;
@BTrace
public class BtraceTest{

    @OnMethod(clazz="com.xxx.yyy.A",method="execute",location=@Location(Kind.RETURN))
    public static void traceExecute(@ProbeClassName String name,@ProbeMethodName String method,int sleepTime){
        println(strcat("the class name=>", name));  
        println(strcat("the class method=>", method));  
        println(strcat("the class method params=>", str(sleepTime)));
    }
}

一切都是正确的。但是:当我将 Thread.sleep(1000) 行移动到 A 类的执行函数时,如下所示:

package com.xxx.yyy;

public class A{
    public void execute(int sleepTime) throws Exception {
        System.out.println("sleep time is "+sleepTime); 
        Thread.sleep(1000);
    }
}

Hello 抛出 NoSuchMethodError。

Exception in thread "main" java.lang.NoSuchMethodError: com.xxx.yyy.A.$btrace$BtraceTest$traceExecute(Ljava/lang/String;Ljava/lang/String;I)V
    at com.xxx.yyy.A.execute(Unknown Source)
    at com.xxx.yyy.Hello.main(Hello.java:8)

我的环境是

java 版本“1.8.0_121”
BTrace v.1.3.9 (20170111)

任何人都可以解释为什么?谢谢!

4

1 回答 1

0

我认为你的班级应该implements Runnableextends Thread。那么只有你可以在你的程序中使用start,sleep和类似的方法。看看这里了解如何实现线程方法。

于 2017-11-13T04:54:22.543 回答