package com.aiks.ip;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
- 实测可用
- 获取当前局域网的所有ip和主机名
- @author 18061
/
public class MyNetIp {
public static void main(String[] args) {NewGetIp1 task1 = new NewGetIp1(); Thread thread1 = new Thread(task1, "线程1"); thread1.start(); NewGetIp2 task2 = new NewGetIp2(); Thread thread2 = new Thread(task2, "线程2"); thread2.start(); NewGetIp3 task3 = new NewGetIp3(); Thread thread3 = new Thread(task3, "线程3"); thread3.start(); NewGetIp4 task4 = new NewGetIp4(); Thread thread4 = new Thread(task4, "线程4"); thread4.start();
}
}
class NewGetIp1 implements Runnable {
private int num = 255;
Lock l = new ReentrantLock();
boolean flag=true;
@Override
public void run() {
// TODO Auto-generated method stub
try {
while (true) {
l.lock();
if (num > 180) {
String name = “192.168.37.”;
name = name + Integer.toString(num);
num–;
// System.out.println(name);
InetAddress ia = InetAddress.getByName(name);
String ip = ia.getHostAddress();
String hostName = ia.getHostName();
if (!ip.toString().equals(hostName.toString())) {
System.out.println(Thread.currentThread().getName()
+ "===" + ip + "=====" + hostName);
System.out.println("-------------------------");
} else {
//System.out.println("*****");
}
}else if(flag){
//这里定义标记,如果该线程完成任务.标记设置为false
System.out.println(Thread.currentThread().getName()+"的总数:"+count);
flag=false;
}
l.unlock();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class NewGetIp2 implements Runnable {
private int num = 180;
Lock l = new ReentrantLock();
boolean flag=true;
@Override
public void run() {
// TODO Auto-generated method stub
try {
while (true) {
l.lock();
if (num > 120) {
String name = "192.168.37.";
name = name + Integer.toString(num);
num--;
// System.out.println(name);
InetAddress ia = InetAddress.getByName(name);
String ip = ia.getHostAddress();
String hostName = ia.getHostName();
if (!ip.toString().equals(hostName.toString())) {
System.out.println(Thread.currentThread().getName()
+ "===" + ip + "=====" + hostName);
System.out.println("-------------------------");
} else {
//System.out.println("*****");
}
}else if(flag){
System.out.println(Thread.currentThread().getName()+"的总数:"+count);
flag=false;
}
l.unlock();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class NewGetIp3 implements Runnable {
private int num = 120;
Lock l = new ReentrantLock();
boolean flag= true;
@Override
public void run() {
// TODO Auto-generated method stub
try {
while (true) {
l.lock();
if (num > 60) {
String name = "192.168.37.";
name = name + Integer.toString(num);
num--;
// System.out.println(name);
InetAddress ia = InetAddress.getByName(name);
String ip = ia.getHostAddress();
String hostName = ia.getHostName();
if (!ip.toString().equals(hostName.toString())) {
System.out.println(Thread.currentThread().getName()
+ "===" + ip + "=====" + hostName);
System.out.println("-------------------------");
} else {
//System.out.println("*****");
}
}else if(flag){
System.out.println(Thread.currentThread().getName()+"的总数:"+count);
flag=false;
}
l.unlock();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class NewGetIp4 implements Runnable {
private int num = 60;
Lock l = new ReentrantLock();
boolean flag=true;
@Override
public void run() {
// TODO Auto-generated method stub
try {
while (true) {
l.lock();
if (num > 0) {
String name = "192.168.37.";
name = name + Integer.toString(num);
num--;
// System.out.println(name);
InetAddress ia = InetAddress.getByName(name);
String ip = ia.getHostAddress();
String hostName = ia.getHostName();
if (!ip.toString().equals(hostName.toString())) {
System.out.println(Thread.currentThread().getName()
+ "===" + ip + "=====" + hostName);
System.out.println("-------------------------");
} else {
//System.out.println("*****");
}
}else if(flag){
System.out.println(Thread.currentThread().getName()+"的总数:"+count);
flag=false;
}
l.unlock();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
近期评论