闲来无事写一个多线程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; // 需验证的IP
private CountDownLatch cdAnswer;
public PingRunner(Map<String, String> hostList, CountDownLatch cdAnswer) {
this.hostList = hostList;
this.cdAnswer = cdAnswer;
}
Socket s = new Socket();
@Override
public 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秒时间