Java 创建用于查询某些设备的线程
我试图从设备中获取数据,并创建了一个类,该类将通过IP属性生成一个套接字。当线程单独启动时,一切都进行得很顺利,但当我用另一个IP启动第二个线程时,两个线程的参数都相同Java 创建用于查询某些设备的线程,java,multithreading,sockets,Java,Multithreading,Sockets,我试图从设备中获取数据,并创建了一个类,该类将通过IP属性生成一个套接字。当线程单独启动时,一切都进行得很顺利,但当我用另一个IP启动第二个线程时,两个线程的参数都相同 public static String addressFiorenza = "192.168.9.201"; //Fiorenza public static String addressFriulmac = "192.168.10.191"; //Friulmac public static int port = 976
public static String addressFiorenza = "192.168.9.201"; //Fiorenza
public static String addressFriulmac = "192.168.10.191"; //Friulmac
public static int port = 9761;
public static void main(String[] args) throws Exception {
Thread friulmac = new threadForMachine(addressFriulmac, port);
friulmac.start();
Thread fiorenza = new threadForMachine(addressFiorenza, port);
fiorenza.start();}
我认为这是个愚蠢的问题,但为什么呢
upd:对不起,这是threadForMachine课程:
public class threadForMachine extends Thread implements Runnable {
private static Socket s;
public static String address;
public static int port;
byte[] requestBothRegTime = {(byte) 0x10, (byte) 0x03, (byte) 0x00, (byte) 0x16, (byte) 0x00, (byte) 0x02, (byte) 0x26, (byte) 0x8E};
byte[] requestForStatus = {(byte) 0x10, (byte) 0x03, (byte) 0x00, (byte) 0x1A, (byte) 0x00, (byte) 0x01, (byte) 0xA6, (byte) 0x8C};
byte[] requestForOnTimes = {(byte) 0x10, (byte) 0x03, (byte) 0x00, (byte) 0x18, (byte) 0x00, (byte) 0x02, (byte) 0x47, (byte) 0x4D};
String inProgress = "3425680";
String inIdle = "1328200";
boolean isStarted;
public threadForMachine(String IPAddress, int port) {
this.address = IPAddress;
this.port = port;
}
public void run() {
System.out.print("Current status of" + address + " is: ");
try {
if (status() == true) {
System.out.println("ONLINE");
} else {
System.out.println("OFFLINE");
}
} catch (IOException ex) {
Logger.getLogger(threadForMachine.class.getName()).log(Level.SEVERE, null, ex);
}
do {
try {
getRequest(address, "Fiorenza");
} catch (IOException ex) {
Logger.getLogger(threadForMachine.class.getName()).log(Level.SEVERE, null, ex);
}
} while (true);
}
private void getRequest(String IP, String machineID) throws IOException {
if (status() == true && isStarted == false) {
isStarted = true;
System.out.println("Time is " + getUptime());
System.out.println("Times on " + getOnTimes());
System.out.println("ITS ALIVE!!!");
}
if (status() == false && isStarted == true) {
isStarted = false;
System.out.println("ITS DEAD!!!!");
}
}
public boolean status() throws IOException {
String status = workFlow(requestForStatus, address, port, 7);
if (status.equals(inProgress)) {
return true;
}
return false;
}
public int getUptime() throws IOException {
int upTime = Integer.parseInt(workFlow(requestBothRegTime, address, port, 7));
return upTime;
}
public int getOnTimes() throws IOException {
int onTimes = Integer.parseInt(workFlow(requestForOnTimes, address, port, 7));
return onTimes;
}
public static String workFlow(byte[] comm, String address, int port, int length) throws IOException {
String dataBlock = "";
try {
byte inp[] = new byte[10];
s = createSocket(address, port);
s.getOutputStream().write(comm);
s.setSoTimeout(1000);
Thread.sleep(250);
s.getInputStream().read(inp);
dataBlock = Integer.toString(fromHex(getTime(inp, length)));
getTime(inp, length);
s.close();
} catch (Exception e) {
System.out.println("init error: " + e.getMessage());
if (e.getMessage().contains("Read timed out")) {
System.out.println("Connection lost. Attempting to reconnect");
s.close();
}
}
return dataBlock;
}
public static Socket createSocket(String address, int port) throws Exception {
Socket socket = new Socket(address, port);
return socket;
}
}
这三个字段不能是静态的。删除静态
关键字
方法公共静态字符串工作流()
也需要是非静态的
另请注意问题显示
threadForMachine的代码
,顺便说一下:我们只能猜测代码的作用和问题所在。我们帮不了你。你能详细说明是什么让你感到困扰,因为两个线程都有相同的论点吗?线程是否可以正常运行或显示任何意外行为?正常。我将按照建议重命名我的类名。我希望它会解决我的问题)顺便说一句,根据研究,我需要了解如何创建两个或两个以上的线程与不同的argu。。。。该死这真是个愚蠢的问题。。。谢谢大家。
private static Socket s;
public static String address;
public static int port;