최근에 CPU 공유와 같은 시스템 정보를 얻기위한 프로젝트를 진행하고 있었지만 Dynamic Link 라이브러리를 사용하고 싶다고 생각했지만 나중에 JNI에 전화하지 않으면 서 다음과 같이 할 수 있다는 것을 알았습니다. 많은 새로운 기술.
Java에서는 전체 물리적 메모리, 나머지 물리적 메모리, 중고 물리적 메모리 등과 같은 정보를 얻을 수 있습니다. 다음 예제는이 정보를 얻고 Windows에서 메모리 사용률을 얻을 수 있습니다.
먼저 MonitorInfobean 클래스를 작성하여 물리적 메모리, 남은 물리적 메모리, 사용한 물리적 메모리, 메모리 사용 및 기타 필드를 포함한 일부 모니터링 정보를로드하십시오.
com.amgkaka. MoniterInfobean { / / /** 메모리는* / /** 남아 있습니다. / private maxmemory; / / ** 운영 체제 메모리. / ** CPU 사용법 setfreephysicalmemorysize (긴 freephysicalmemorysize) {freephysicalmemorysize; } 공개 void setosname (String osname) {osname = Osname; 공개적으로 anttotalmorysize (긴 총 메모리 크기) {totalmemorysize = gettotalthread () {return the alltotalthread (int totalthread) {this. totalthread; } public void setusedMemory (Long UsedMemory) {this .useedmemory = indecpuratio (return cpuratio) {this .cpuratio = cpuratio;
그런 다음 인터페이스를 작성하여 현재 모니터링 정보를 얻으십시오.
Com.amgkaka. Public Interface ImonitorService { /** * / /** * 현재 모니터링 개체를 받으십시오. : 08 */ public monitorInfobean getMonitorInfobean ()은 예외를 던집니다.
이 클래스의 구현 클래스 MonitorServiceImpl은 다음과 같습니다.
com.amgkaka 비즈니스 로직 구현 클래스. * @author amg * @version 1.0 생성 날짜 : 2008-3-11-10:06:06 AM */public class monitorserviceimpl imonitorservice {//이 시스템을 읽고 실행하기 위해 더 오래 설정할 수 있습니다. CPU 점검 비율은 개인 정적 최종 결점 = 100; * @return 구성 모니터링 개체 * @Throws Exception * @Author AMG * 생성 날짜 : 2008-4-25-10:45:08 AM */ Public MonitorInfobean getMonitorInfobean () EXC 에피소드 {int KB = 1024; 메모리는 Long TotalMemory = runtime.getRuntime (). TotalMemory () / kb; getRuntime () .maxmemory () / kb; .GetTotalPhysicalMemorySize () / KB; Threads readsgroup parentthread를 얻습니다. Double Cpuratio = 0; 프리 메모리 크기); 사수화 (TotalThread) ); : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :. *// ** * CPU 사용을 받으십시오. * @return CPU 사용법 * @Author AMG * 생성 날짜 : 2008-4-25-06:05:11 PM */ Private Double GetCpuratioforWindows () {String Proccmd = System. .getenv ( "windir") + "//system32//wbem//wmic.exe 캡션, Commandlin e," + "kernelmodetime, readoperationcount, usermodetime, writeOperationCount"; = runtime.getRuntime (). exec (proccmd); ! = null) {long idletime = c1 [0] [0]; (); else {ex. @Author AMG * 생성 날짜 : 2008-4-25-06:10:14 PM */ Private Long [] readCPU (최종 프로세스 Proc) {long [] retn = New Long [2]; .Close (); inputStreamReader ir = inputStreamReader (proc.getInputStream ()); <반환 null} int cmdidx = line.indexof (int rocidx = line.indexof (usermodeTime ") input.readline ()! = null) {if (line.length () <wocidx) {계속; BYTES.SUBSTRING (LINE, CAPIDX, CMDIDX -1) .trim (); SubString (line, cmdid x, kmtidx -1). > = 0) {계속; .valueof (bytes.substring (line, kmtidx, rocidx -1) .trim () .longvalue (); longvalue (); , wocidx -1) .longvalue (); {proc.getInputStream ().}} {e.printstacktrace ()}; 예외 * @Author AMG * 생성 날짜 : 2008-4-30-04:47:29 PM */ public static void main (String [] args)은 예외 {im onitorservice service = new monitorserviceimpl (); .getMonitorInfobean (); (남은 메모리 = " + monitorInfo.getFreememory ()); ()); System.out.println ( "총 물리적 메모리 =" + MonitorInfo.getTotalMemorySize () + "KB"); ); System.out.println (물리적 메모리 사용 = " + monitorInfo.getUsedMemory () +"kb "); }}
이 구현 클래스는이 클래스의 코드를 직접 작성하는 도구 클래스가 필요합니다.
com.amgkaka. /** * / /** * string.substring은 중국어 처리에 문제가 있으므로 (중국어를 바이트로 고려) * @param src intercepted와 같은 조정이 포함됩니다 문자열 * @param start_idx start codinatine (이 좌표 포함) * @param end_idx 컷오프 좌표 (이 좌표 포함) * @return */ public static string substring (String Src, Int Start_idx, int en d_idx) {byte [] b = src .getBytes ();
MonitorBeanImpl 클래스를 실행하면 리더는 현재 메모리, CPU 사용 및 기타 정보를 볼 수 있습니다.
추신 : LAN에서 모든 호스트 이름을 얻는 메소드
import java.net.inetaddress; import java.net.unknownhostexception; public class a {static public void main (string [] args) {try {// 호스트 이름 netAddress address = inetAddress.getByName ( "192.168 .9.148 "); System.out.println ("192.168.9.148 "+": "+address.gethostAddress ()); // IP 문자열 ips ="192.168.9. ", IP; inetAddress addip을 통해 호스트 이름을 가져옵니다. (int i = 148; i ++) {IP = IPS+i; catc h (unknownhostexception uhe) {system.err.println ( "+"192.168.9.148 ");