本文為大家分享了模擬ping操作的一個java類,具體內容如下
導入java.io.ioexception;導入java.net.inetaddress;導入java.net.unknownhostexception; /** *由Qiuju *於2014/9/21創建。 */公共類簡單實現運行{私有最終對象mendlock = new Object();私人布爾iSend = false;私人int到達= 0;私人INT計數;私人int超時;私有字符串名稱;私人int mendcount;私有字符串mip = null;私有浮子mlossrate = 1f;私有浮點mdelay = 0;公共簡單(字符串名稱,int count,int timout){count = mendCount = count;超時=超時;名稱=名稱; for(int i = 0; i <mendcount; i ++){thread thread = new thread(this); thread.setdaemon(true); thread.start(); } if(!isend){try {synchronized(mendlock){mendlock.wait(); }} catch(InterruptedException e){e.printstacktrace(); }}}} private void steTend(boolean isarried,長延遲,字符串ip){同步(mendlock){count---;如果(已多默接){到達++; mdelay =(mdelay + delay) / 2f; if(ip!= null)mip = ip; }} if(count == 0)setend(); } private void setend(){mlossrate =(MendCount -resercount) / mendcount; isend = true;同步(Mendlock){Mendlock.notifyall(); }} @Override public void run(){long delay = 0;布爾人屬於= false;字符串ip = null;嘗試{long starttime = system.currenttimemillis(); InetAddress地址= InetAddress.getByName(name); isarried = address.israchable(超時); delay = system.currentTimeMillis() - 啟動時間; ip = address.gethostaddress(); } catch(unknownhostexception e){e.printstacktrace(); } catch(ioexception e){e.printstacktrace(); } catch(異常E){e.printstacktrace(); }最後{setend(已交配,延遲,ip); }} public String getip(){return mip; } public float getLossrate(){返回mlossrate; } public float getDelay(){return mdelay; } public boolean getIssucceed(){返回到達> 0; }}}在類中使用的是:
長啟動時間= system.currentTimeMillis(); InetAddress地址= InetAddress.getByName(name); isarried = address.israchable(超時); delay = system.currentTimeMillis() - 啟動時間; ip = address.gethostaddress();
其中的:address.israchable(超時);方法
之所以說是中間類,也就是因為這個原因沒有採用這個類。 ,也就是因為這個原因沒有採用這個類。
以上就是本文的全部內容,希望對大家的學習有所幫助。 ,希望對大家的學習有所幫助。