正常运行时代码不起作用,但在调试模式下(eclipse)可以正常工作。

10

我真的很困惑:当我在Eclipse中正常运行程序时,我的某些代码不起作用,但是当我使用调试模式逐步运行每个步骤时,它确实有效。

代码:

public void showConnectDialog() {
    ConnectDialog connectDialog = new ConnectDialog();
    connectDialog.setVisible(true);
    //Until here, code runs
    while(! connectDialog.getConnected()) {};
    //The next line does only run in debug
    JOptionPane.showMessageDialog(connectDialog, "Connected", "Connected", JOptionPane.INFORMATION_MESSAGE);

}

连接器(在用户点击对话框中的“连接”时作为线程启动):
private class ServerConnector implements ActionListener, Runnable {

    @Override
    public void actionPerformed(ActionEvent e) {
        if (! IP_field.getText().equals("")) {
            if (! isConnecting) {
                new Thread(new ServerConnector(), "ServerConnector").start();

            }

        }
        else {
            JOptionPane.showMessageDialog(dialog, 
                                          "Enter an IP address", 
                                          "Enter IP", 
                                          JOptionPane.WARNING_MESSAGE);

        }

    }

    @Override
    public void run() {
        try {
            setConnecting(true);
            Socket socket = connect();
            if (socket != null) {
                ObjectOutputStream oOut = new ObjectOutputStream(socket.getOutputStream());
                ObjectInputStream oIn = new ObjectInputStream(socket.getInputStream());
                if (login(oOut, oIn)) {
                    isConnected = true;
                    setConnecting(false);

                }
                else {
                    socket.close();

                }

                setConnecting(false);

            }

        }
        catch (RSPException e) {
            e.printStackTrace();
            System.exit(1);

        }
        catch (Exception e) {
            //If an exception occurs, setConnecting() will be true. This 
            //not good, so it has to be set to false
            e.printStackTrace();
            setConnecting(false);

        }

    }

    private boolean login(ObjectOutputStream oOut, ObjectInputStream oIn) 
            throws ClassNotFoundException, IOException, RSPException {
        //Send login request action:
        oOut.writeObject(new LoginAction(ActionSender.CLIENT, getID(), 
                                         getPassword()));

        Object obj = oIn.readObject();
        if (obj instanceof LoginActionResult) {
            LoginActionResult result = (LoginActionResult) obj;
            if (result.getResult() == LoginResults.SUCCES) {
                return true;

            }
            else if (result.getResult() == LoginResults.FAIL_ON_ID) {
                JOptionPane.showMessageDialog(dialog, 
                                              "Invalid password or ID", 
                                              "Can't login", 
                                              JOptionPane.ERROR_MESSAGE);
                return false;

            }
            else if (result.getResult() == LoginResults.FAIL_ON_PASSWORD) {
                JOptionPane.showMessageDialog(dialog, 
                                              "Invalid password or ID", 
                                              "Can't login", 
                                              JOptionPane.ERROR_MESSAGE);
                return false;

            }
            else if (result.getResult() == LoginResults.SERVER_FULL) {
                JOptionPane.showMessageDialog(dialog, 
                                              "Couldn't connect: \n" +
                                              "Server is full", 
                                              "Failed to connect", 
                                              JOptionPane.WARNING_MESSAGE);
                return false;

            }
            else {
                return false;

            }

        }
        else {
            System.out.println(obj);
            throw new RSPException("Server is not following the protocol.");

        }

    }

    private void setConnecting(boolean connecting) {
        if (connecting) {
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    connectButton.setEnabled(false);

                }
            });
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    connectButton.setText("Connecting...");

                }
            });

        }
        else {
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    connectButton.setText("Connect");

                }
            });
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    connectButton.setEnabled(true);

                }
            });

        }

        isConnecting = connecting;

    }

    private String getAddressFromTextField() {
        return IP_field.getText();

    }

    private InetAddress getInetAddress(String fullAddress) {
        try {
            if (fullAddress.contains(":")) {
                String[] splitAddress = fullAddress.split(":");
                return InetAddress.getByName(splitAddress[0]);

            }
            else {
                return InetAddress.getByName(fullAddress);

            }
        }
        catch (UnknownHostException e) {
            return null;

        }

    }

    private int getPort(String fullAddress) {
        try {
            String[] splittedAddress = fullAddress.split(":");
            return Integer.valueOf(splittedAddress[1]);

        }
        catch (NumberFormatException ex) {
            return -1;

        }
        catch (NullPointerException 
             | ArrayIndexOutOfBoundsException 
             | PatternSyntaxException ex) {
            //Returning default port value: 25566, because no port was given
            return 25566;

        }

    }

    @SuppressWarnings("resource")
    private Socket connect() {
        Socket socket = null;

        InetAddress address = null;
        if ((address = getInetAddress(getAddressFromTextField())) == null) {
            return null;

        }
        int port = getPort(getAddressFromTextField());

        try {
            socket = new Socket(address, port);

        }
        catch (ConnectException e ) {
            Socket retrySocket = null;
            if ((retrySocket = retryConnect(address, port)) == null) {
                JOptionPane.showMessageDialog(dialog,
                                              "Connection timed out", 
                                              "Failed to connect", 
                                              JOptionPane.ERROR_MESSAGE);
                setConnecting(false);

            }
            else {
                socket = retrySocket;

            }

        }
        catch(IOException e) {
            e.printStackTrace();

        }

        return socket;

    }

    private Socket retryConnect(InetAddress address, int port) {
        Thread waitThread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    //Will wait 15(000) (milli)seconds before stopping with
                    //trying to connect.
                    //One second (1000 millis) is for debugging and testing
                    Thread.sleep(1000);

                }
                catch (InterruptedException e) {
                    e.printStackTrace();

                }

            }

        });

        waitThread.start();

        while (waitThread.isAlive()) {
            try {
                return new Socket(address, port);

            }
            catch (ConnectException e) {
                //Do nothing, will re-attempt to connect.

            }
            catch (IOException e) {
                e.printStackTrace();

            }

        }

        return null;

    }

    private String getID() {
        return ID_field.getText();

    }

    private String getPassword() {
        if (getID().equals("master")) {
            return "masterPassword";

        }
        else {
            return new String(passwordField.getPassword());

        }

    }

}

getConnected()会在连接到服务器后立即返回true。连接器在一个单独的线程上运行。

编辑: 我试图在getConnected()函数中添加代码,然后它就可以正常工作了。为什么这时可以而其他情况不行呢?


3
可能是竞态条件。你可以发布ConnectDialog的代码吗? - austin
你在断点期间评估任何表达式吗?这些评估实际上可以改变程序的状态(也许你很幸运,它将其改变为工作状态?) - David says Reinstate Monica
@austin ConnectDialog的代码有700行,所以我只会发布实际连接器的代码(仍然非常长...)希望你没问题。 - cvbattum
你可以在这里使用waitnotifyshowConnectDialog会等待ServerConnector线程通知它。这样你就可以避免while循环,使程序不会阻塞CPU核心。 - Tom
你没有展示出 isConnecting 变量的定义位置... 它被定义为 volatile 了吗?https://docs.oracle.com/javase/tutorial/essential/concurrency/atomic.html - Nate
我确认,thread.sleep在我使用的情况下确实有效,但参数设置为20毫秒,非常感谢您的帮助!希望这能帮助其他人。 - WizziLife
4个回答

5
我遇到了同样的问题,但是有更多的细节。代码在32位上运行良好,但在64位上出现了问题(我使用本地库,因此需要同时维护两者)。
我找到的解决方法是在while循环中添加Thread.sleep()。我不知道为什么它可以解决问题,所以你的猜测和我的一样好。
一个更好的解决方案可能是实现观察者模式,而不是使用无限循环。但这需要进行一些重构。

3
使用Thread.sleep()可以解决问题,但这不是一个很好的方法。相反,我们应该使用Thread.yield()
为什么使用yield而不是sleep
参考: Thread.Sleep(0)和Thread.Yield()之间的区别以及Thread.sleep(0)和Thread.yield()语句是否等效? 为什么这有效?
当我们只运行线程时,操作系统将它们置于“空闲”状态,当它期望“唤醒”时,却没有发生。另一方面,在调试模式下,我们有一个可控制的环境。操作系统对其的控制很少,因为所有事情都是逐步进行的。如果我们连续运行几次调试,没有任何断点,经过几次成功运行后,我们应该看到相同的效果。

2

我曾经遇到过一个类似的问题,那就是while循环不会运行,而该循环是我的主程序。我如何让循环运行起来呢?方法是在循环中的第一件事情就是让它睡眠:

    try
        {Thread.sleep(0);}
    catch (Exception e)
        {e.printStackTrace();}

这已经足够让一切开始了。

1

我在UIAutomator中遇到了与UiObject2 wait(Until.findObject(),20)相同的问题。

Thread.yield() - 对我有用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接