3

我有这个 Java Swing 应用程序,每次传入客户端尝试建立连接时,它都会启动一个新线程,该线程使用执行程序池打开套接字服务器。

该应用程序需要两个按钮,一个用于启动,另一个用于停止服务器。我想要的是显示服务器状态,并禁用相反的按钮,直到其状态改变。

这就是我现在所拥有的,但我不知道当线程停止时如何与 EDT 通信。我能做的就是检查isRunning()方法。

使用 SwingWorker 会更好吗?

public class ServerManager implements Runnable {

    private Executor mExecutor          = Executors.newSingleThreadExecutor();
    private ServerSocket mServerSocket  = null;
    private int mDefaultPort            = 43012;    
    private volatile boolean isRunning  = false;

    public ServerManager (int port){
        mDefaultPort = port;        
    }

    @Override
    public void run() {
        try {           
            mServerSocket = new ServerSocket(mDefaultPort);
            isRunning = true;
            while (isRunning){
                mExecutor.execute(new IncomingClientThread(mServerSocket.accept()));
            }
        } catch (IOException e) {           
            e.printStackTrace();
        } finally {
            if(mServerSocket != null){
                stop();
                System.out.println("Server closed");
            }
        }
    }

    public void stop(){
        try {
            mServerSocket.close();
            isRunning = false;
        } catch (IOException e) {
            throw new RuntimeException("Error closing server", e);
        }
    }

    public synchronized boolean isRunning() {
        return isRunning;
    }

    public int getServerPort (){
        return mDefaultPort;
    }
}

这就是我在 GUI 线程中的内容:我只使用一个按钮并在每次按下它时更改其文本,但如果服务器由于某种原因断开连接,则按钮保持不变。

connectButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if(mServer.isStopped()){
                    new Thread (mServer).start();
                    connectButton.setText("Desconectar");
                    infoLabel.setText("Servidor online en IP: " + NetworkUtils.getLocalIpAddress()
                            + " puerto: " + mServer.getServerPort());
                    System.out.println(mServer.getIpAddress());
                }else{
                    mServer.stop();
                    connectButton.setText("Conectar");
                    infoLabel.setText("Offline");
                }
            }
        });

欢迎任何帮助!谢谢。

4

2 回答 2

4

一种可能的解决方案是给它一个 SwingWorker 的部分功能——给它一个 SwingPropertyChangeSupport 对象,并允许你的 GUI 监听和响应状态变化。

例如,

public class ServerManager implements Runnable {
  public static final String IS_RUNNING = "is running"; // for the Event's name
  private SwingPropertyChangeSupport propChngSupport = new SwingPropertyChangeSupport(this);
  private volatile boolean isRunning  = false;
  // other variables

  // addPropertyChangeListener(...) {...} goes here
  // removePropertyChangeListener(...)  {...} goes here

  public void setIsRunning(boolean isRunning) {
    boolean newValue = isRunning;
    boolean oldValue = this.isRunning;
    this.isRunning = isRunning;
    propChngSupport.firePropertyChange(IS_RUNNING, oldValue, newValue);    
  }

  public void run() {
    // ....
  }

  // other methods
}

关键是永远不要在其 setter 方法之外更改 isRunning 属性。

于 2012-10-08T22:59:56.917 回答
0

所以,我按照@Hovercraft Full Of Eels 的建议所做的是:

在线程类中:

    public class ServerManager implements Runnable {

...     
        public static final String IS_RUNNING = "IS_RUNNING";

        private SwingPropertyChangeSupport pChange = new SwingPropertyChangeSupport(this);


        public void setIsRunning (boolean isRunning){
            boolean newValue = isRunning;
            boolean oldValue = this.isRunning;
            this.isRunning = isRunning;
            pChange.firePropertyChange(IS_RUNNING, oldValue, newValue);         
        }   

        public void addPropertyChangeListener (PropertyChangeListener listener){
            pChange.addPropertyChangeListener(IS_RUNNING, listener);
        }

        public void removePropertyChangeListener(PropertyChangeListener listener){
            pChange.removePropertyChangeListener(IS_RUNNING, listener);
        }
...
    }

在 GUI 类中:

public class StatusPane extends JPanel{

    private ServerManager mServer;

    public StatusPane() {       
    ...
        mServer = new ServerManager();

        mServer.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                System.out.println(evt.getPropertyName() + " cambia su valor de " 
                        + evt.getOldValue() + " a " + evt.getNewValue());
            }
        });
    ... 
    }
}
于 2012-10-09T09:08:36.567 回答