【问题标题】:Creating threads for interrogate some equipmet为询问某些设备创建线程
【发布时间】:2019-10-29 13:53:34
【问题描述】:

我正在尝试从设备和创建的类中获取数据,该类将通过属性 IP 给定一个套接字。由于线程单独启动,一切顺利,但是当我使用另一个 IP 启动第二个线程时,两个线程都得到相同的参数。

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的代码
  • 我们只能猜测代码做了什么,问题是什么。我们无法帮助您。
  • 您能否详细说明两个线程都获得相同参数时困扰您的问题?线程能否正常运行或显示任何意外行为?
  • 好的。我将按照建议重命名我的类名。我希望它能解决我的问题)顺便说一句,根据情况,我需要了解如何创建两个或多个具有不同参数的线程......该死......这真是个愚蠢的问题......谢谢你们。

标签: java multithreading sockets


【解决方案1】:
private static Socket s;
public static String address;
public static int port;

这三个字段不能是静态的。删除static 关键字。

方法public static String workFlow()也需要是非静态的。

另见this answer问题What does the 'static' keyword do in a class?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    • 2018-04-01
    • 2021-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多