老规矩,先看测试代码,测试代码很简单,每隔 100ms 运行一次 sayHi 方法,并随机随眠一段时间。
packageorg.xunche.app;publicclassHelloTraceAgent{publicstaticvoidmain(String[] args)throwsInterruptedException{HelloTraceAgent helloTraceAgent =newHelloTraceAgent();while(true) {helloTraceAgent.sayHi("xunche");Thread.sleep(100); } }publicStringsayHi(String name)throwsInterruptedException{ sleep();String hi ="hi, "+ name +", "+ System.currentTimeMillis();returnhi; }publicvoidsleep()throwsInterruptedException{Thread.sleep((long) (Math.random() *200)); }}