多线程PING IP+端口号!(源码),附非多线程时间测试对比。。
来源:互联网 发布:百度怎么推广淘宝店铺 编辑:程序博客网 时间:2024/05/17 07:51
闲来无事写一个多线程PING IP+端口号的demo,希望可以得到大家的指教,如有不足请指出,万分感谢!!!
package com.card.test;import java.io.IOException;import java.net.InetSocketAddress;import java.net.Socket;import java.net.SocketAddress;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class MultithreadPingIp {private List<Map<String, String>> ipList; // 需验证的IP+端口集合private int threadNum = 0; // 线程数public MultithreadPingIp(List<Map<String, String>> ipList, int threadNum) {this.ipList = ipList;this.threadNum = threadNum;}public MultithreadPingIp() {}public void startPing() {// 创建一个线程池,多个线程同步执行final CountDownLatch cdAnswer = new CountDownLatch(threadNum);ExecutorService pool = Executors.newFixedThreadPool(threadNum);for (int i = 0; i < ipList.size(); i++) {pool.execute(new PingRunner(ipList.get(i), cdAnswer));}try {System.out.println("线程等待开始=" + cdAnswer.getCount());cdAnswer.await();System.out.println("线程等待结束=" + cdAnswer.getCount());} catch (InterruptedException e) {}pool.shutdown();try {while (!pool.isTerminated()) {Thread.sleep(100);}} catch (Exception e) {e.printStackTrace();}}private class PingRunner implements Runnable {private String host = null;private int port = 0;private Map<String, String> hostList; // 需验证的IPprivate CountDownLatch cdAnswer;public PingRunner(Map<String, String> hostList, CountDownLatch cdAnswer) {this.hostList = hostList;this.cdAnswer = cdAnswer;}Socket s = new Socket();@Overridepublic void run() {host = hostList.get("addr");port = Integer.valueOf(hostList.get("port"));SocketAddress add = new InetSocketAddress(host, port);try {s.connect(add, 3000);// 超时3秒System.out.println("IP:" + host + "正常");} catch (IOException e) {System.out.println("IP:" + host + "超时");} finally {try {s.close();cdAnswer.countDown();} catch (IOException e) {}}}}public static void main(String[] args) {long currentTime = System.currentTimeMillis();SimpleDateFormat formatter = new SimpleDateFormat("HH时mm分ss秒");Date date = new Date(currentTime);System.out.println("多线程测试:Ping开始时间:" + formatter.format(date));List<Map<String, String>> allIp = new ArrayList<Map<String, String>>();Map<String, String> m = new HashMap<String, String>();Map<String, String> m1 = new HashMap<String, String>();Map<String, String> m2 = new HashMap<String, String>();Map<String, String> m3 = new HashMap<String, String>();Map<String, String> m4 = new HashMap<String, String>();Map<String, String> m5 = new HashMap<String, String>();m.put("addr", "192.168.1.1");m.put("port", "8080");m1.put("addr", "192.168.1.2");m1.put("port", "8080");m2.put("addr", "192.168.0.110");m2.put("port", "1521");m3.put("addr", "192.168.0.111");m3.put("port", "1521");m4.put("addr", "192.168.0.112");m4.put("port", "1521");m5.put("addr", "192.168.0.113");m5.put("port", "1521");allIp.add(m);allIp.add(m1);allIp.add(m2);allIp.add(m3);allIp.add(m4);allIp.add(m5);MultithreadPingIp batchPingIpThread = new MultithreadPingIp(allIp,allIp.size());//多线程测试PING IP+端口时间batchPingIpThread.startPing();//非多线程测试PING IP+端口时间//batchPingIpThread.ipConnec(allIp);long current = System.currentTimeMillis();Date end = new Date(current);System.out.println("多线程测试:Ping结束时间:" + formatter.format(end));}//非多线程实现public void ipConnec(List<Map<String, String>> list) {for (int i = 0; i < list.size(); i++) {Socket s = new Socket();String host = list.get(i).get("addr");int port = Integer.valueOf(list.get(i).get("port"));SocketAddress add = new InetSocketAddress(host, port);try {try {s.connect(add, 3000);// 超时3秒System.out.println("Ip:" + host + "正常!");} catch (IOException e) {System.out.println("Ip:" + host + "超时!");}} finally {try {s.close();} catch (IOException e) {}}}}}
多线程测试结果:
Ping开始时间:18时34分05秒
线程等待开始=6
IP:192.168.0.110正常
IP:192.168.1.1超时
IP:192.168.0.112超时
IP:192.168.1.2超时
IP:192.168.0.111超时
IP:192.168.0.113超时
线程等待结束=0
Ping结束时间:18时34分08秒
可以看出ping六个IP相差3秒时间
单线程测试结果:
Ping开始时间:18时45分22秒
Ip:192.168.1.1超时!
Ip:192.168.1.2超时!
Ip:192.168.0.110正常!
Ip:192.168.0.111超时!
Ip:192.168.0.112超时!
Ip:192.168.0.113超时!
Ping结束时间:18时45分33秒
可以看出ping六个IP相差11秒时间
阅读全文
0 0
- 多线程PING IP+端口号!(源码),附非多线程时间测试对比。。
- win32.多线程程序设计(内附源码)
- 一秒钟检测一个局域网网段--VB.NET通过异步Ping方法测试局域网通断(非多线程)
- 使用pyhton采用多线程方式ping IP
- linux ping IP地址与telnet 测试IP端口
- linux ping IP地址与telnet 测试IP端口
- linux ping IP地址与telnet 测试IP端口
- Android/java http多线程断点下载(附源码)
- Android中的多线程编程(一)附源码
- java多线程核心技术梳理(附源码)
- Java 多线程核心技术梳理(附源码)
- Java中PING IP+端口号!
- 多线程设置超时 测试端口是否打开
- c#多线程ping实战
- 多线程Ping的解决
- 多线程ping代码
- 多线程超级Ping
- 一个多线程Ping 类
- ArrayList和Vector的区别,HashMap和Hashtable的区别。
- 穿插纸条 (第73-90关)终点和连通性对于拐点的限制
- mongodb 误删除集合恢复 误删除表数据恢复
- 自制控件,类型“Appliaction.dd”中不存在类型名称“dd”
- bzoj3122 [Sdoi2013]随机数生成器(bsgs+扩欧+数列)
- 多线程PING IP+端口号!(源码),附非多线程时间测试对比。。
- 虚幻引擎4系列教程2(霜之小刀)(附视频)--第一人称场景建模
- hbase客户端api--建表
- 随笔:浏览器解析动态请求返回页面过程分析
- hdu-2063-过山车-二分匹配-匈牙利-java
- Kali Linux入门第一课 | 内网抓取用户浏览图片
- leetcode171. Excel Sheet Column Number
- app分析 名词解释 app购买量
- VS2013配置Boost库