最近、私はCPU共有などのシステム情報を取得するためのプロジェクトに取り組んでいました。多くの新しいテクノロジー。
Javaでは、物理メモリ全体、残りの物理メモリ、使用された物理メモリなどの情報を取得できます。次の例では、この情報を取得して、Windowsの下でメモリ使用率を取得できます。
まず、モニターインフォービーンクラスを作成して、物理メモリ、残りの物理メモリ、使用された物理メモリ、メモリの使用量、その他のフィールドを含むいくつかの監視情報をロードします。
パッケージcom.amgka.performance; MonitorInfobean { / /**プライベート長いメモリ。 /プライベートマックスメモリメモリ***/ **プライベート長いメモリ。 / ** CPUの使用setfreephysicalmemorysize(長いfreephysicalmemorysize = freephysicalmemorysize; } public void setosname(this .osname = osname; Public void settotalmemorysize {this totalmemorysize = public gettalthread() } public setusedmemory(long readmemory){edusememory = usedmemory;
次に、このクラスのコードを取得するためのインターフェイスを作成します。
パッケージcom.amgka.performance;パブリックインターフェイスIMONITORSERVICE { /** * / /** *現在の監視オブジェクトを返す * :08 */ public Monitorinfobean getMonitorInfobean()スロー例。
このクラスの実装クラスMonitorServiceImplは次のとおりです。
com.Amgka.io.inputStreamederのパッケージビジネスロジック実装クラス。チェック中のCPUの占有率は正確ではありません。 * @return構築された監視オブジェクトを返します * @throws例外 * @author amg *作成日:2008-4-25-10:45:08 am */ public monitorinfobean getMonitorinfobeanメモリは、long totalmemory = runtime()。totalmemory() / kb; getRuntime().Maxmemory() / KB; .gettalphysicalmemorysize() / Kb; threads threadsthread; for(parentthread.getparent()!= null; parethread = pa rentthread.getparent(); ; double cpuratio = 0; freememory.setfisicalmemory.setmaxmemory(maxmemory.setosname(osname);サイズ); infobean.settotalthread(totalthread ); infobean.setusememory(usedmemory); setcpuratio( :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::づ 火::::: ::::::::::::::::::::::::::::: cpuratio); *// ** * @return return cpu使用量 * @author amg *作成日:2008-4-25-06:05:11 */ private double getcpuratioforwindows(){try {string proccmd = system .getEnv( "Windir") + "//system32//wbem//wmic.exeプロセスキャプション、commandlin e、" + "kernelmodetime、readoperationcount、swreatcount、usermodetime、writeoperationcount"; = readcpu(runtime() != null){long idletime = c1 [0] - busytime = c1 [1]; (); @author amg *作成日:2008-4-25-06:10:14 PM */ private long [] readcpu(final Process proc){long [] retn = new long [2]; .close(); new inputstreamreader()linenumbereder = new linenumberrea der(ir); <faultLength){null; (usermodetime ") input.readline()!= null){if(line.length()<wocidx){continue;} bytes.substring(capidx、cmdidx -1).trim(); > = 0){続行; .valueof(bytes.substring(line、kmtidx、rocidx -1).trim()).longvalue(); longvalue(); 、wocidx -1).longvalue(); {proc.getInputStream()。例外 * @author amg *作成日:2008-4-30-04:47:29 PM */ public static void main(string [] args)スロー例.getMonitorInfobean(); ( "残りのメモリ=" + monitorinfo.getFreeMemory()); ()) ; system.out.println( "Physical Memory red" + monitorinfo.getusedmemory() + "kb"); ;
この実装クラスには、このクラスのコードを次のように書き込むツールクラスが必要です。
パッケージcom.amgka.performance; /** * / /** String.Substringには、漢字処理の問題があるため(漢字をバイトと見なします)、 * @Param SRCがインターセプトされている場合、 * estasmentsが含まれますstring * @param start_idx start coordinate(この座標を含む) * @param end_idxカットオフ座標(この座標を含む) * @return */ public static string substring(string src、int start_idx、int en d_idx){byte [] b = src .getBytes();
MonitorbeanImplクラスを実行すると、読者は現在のメモリ、CPU使用率、その他の情報を確認します。
PS:LANですべてのホスト名を取得する方法
Import java.net.inetAddress; Import java.net.unknownhostexception; public class a {static public void main(string [] args){//ホスト名netaddressアドレス= inetaddress.getbyname( "192.168を介してIPアドレスIを取得します.9.148 "); system.out.println(" 192.168.9.148 "+": "+address.gethostaddress()); // ip string ips =" 192.168.9。 "を介してホスト名を取得します。 ; for( int i=148;i<255;i++){ ip=ips+i; addip=InetAddress.getByName(ip); System.out.println(ip+": "+addip.getHostName()); } } catc h(unknownhostexception uhe){system.err.println( "+" 192.168.9.148 ")}}}