java如何監控乙個方法的執行時間 問題點數

2021-06-22 02:05:33 字數 1726 閱讀 2393

在方法前定義 long time = system.currenttimemillis();

方法執行中...

system.out.println(system.currenttimemillis() - time);//此句是把方法的總執行時間列印在控制台

實現invocationhandler,

開啟執行緒呼叫你的這個方法,然後需要怎麼控制都ok。 

這樣也不用修改你原來的類。

invocationhandler handler = new myinvocationhandler(...);

class proxyclass = proxy.getproxyclass(

foo.class.getclassloader(), new class );

foo f = (foo) proxyclass.

getconstructor(new class ).

newinstance(new object );

foo.class就是你想要監聽的原來那個類,

myinvocationhandler就是你新寫的類,其中需要實現

public object invoke(object proxy, method method, object args) throws

throwable 

((***) proxy).releaseresource(); //到時間釋放資源,不管前面method方法是否完成,比如關閉連線等}}

th.start();

}else 

}後來想著用兩個線程式處理:

class mythread extends threadcatch(exception e)}}

}class demonthread extends thread

public void run()catch(exception e)

system.out.println("demonthread wakeup");

this.tth.stop();

system.out.println("***************==demonthread  end ");}}

public class testthread 

}試著用interrupt(),沒有成功,只能用stop().但是這種過時的方法,api不推薦用,我也不清楚占用的資源有沒有被成功的釋放,

後來發現有個包apache.commons.httpclient裡有個提供這種功能的方法 ,就去試了一下,結果成功了,呵呵,

下面說說我用的測試:

//這裡是設定要用的限制時間

client.setparams(cparams);

getmethod method = new getmethod(urlsource);

//url位址,也就是你要處理的網路資源

method.setfollowredirects(true);

trycatch(exception e){}

finally

略談如何建立乙個監控執行緒

一般實時監控功能放在乙個單獨開闢的執行緒比較合適,因為這樣可以大大減輕主線程的負擔。下面我談談如何建立乙個監控執行緒 以乙個工程說明 使用vs 2005 新建乙個對話方塊工程 test 在 bool c test dlg oninitdialog 函式裡建立執行緒,具體 如下 dword dwthr...

如何執行乙個Vue專案

一開始很多剛入手vue.js的人,會扒github上的開源專案,但是發現不知如何執行github上的開源專案,很尷尬。通過查閱網上教程,成功搭建好專案環境,同時對前段工程化有了朦朦朧朧的認知,因此將環境搭建過程分享給大家。首先,列出來我們需要的東西 node.js環境 npm包管理器 vue cli...

如何執行乙個Vue專案

一開始很多剛入手vue.js的人,會扒github上的開源專案,但是發現不知如何執行github上的開源專案,很尷尬。通過查閱網上教程,成功搭建好專案環境,同時對前段工程化有了朦朦朧朧的認知,因此將環境搭建過程分享給大家。首先,列出來我們需要的東西 node.js環境 npm包管理器 vue cli...