简体   繁体   English

如何在另一个类中杀死一个类的实例?

[英]How do I kill an instance of a class in another class?

I'm making a client-server application, in which the user can turn on or switch off the server from a gui; 我正在创建一个客户端 - 服务器应用程序,用户可以从gui打开或关闭服务器; in order to let it work, I use a SwingWorker nested class. 为了让它工作,我使用SwingWorker嵌套类。 All seems to work correctly, but when I switch off the server and re-turn it on it doesn't work, probably because there is still another instance open of it that can't be overwritten: server is blocked on the accept() method. 所有似乎都正常工作,但当我关闭服务器并重新打开它不起作用,可能是因为还有另一个打开的实例无法覆盖:服务器在accept()上被阻止方法。 I want to kill the previous instance when the user presses the switch off button, but I don't know how. 当用户按下开关按钮时,我想杀死前一个实例,但我不知道如何。

Here's the SwingWorker class that gives me problems: 这是给我带来问题的SwingWorker类:

class SwingWorkGUI extends SwingWorker
    {
        @Override
        protected Integer doInBackground() throws Exception {
            int delay = 1000; 
            final Timer time = new Timer(delay, null);
            time.addActionListener( new java.awt.event.ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (Server.up == true){
                        upTimeMillis += 1000;
                        long diff = upTimeMillis/1000;
                        long numHrs = diff/60;
                        diff = diff%60;
                        long numMins = diff/60;
                        long numSecs = diff%60;
                        upTime.setText(numHrs + ":" + numMins + ":" + numSecs);
                    }
                    else {
                        upTime.setText("Server Actually Down");
                        time.stop();
                    }
                }           
            });
            time.start();
            mainServer = new Server();
            return null;
        }
    }

Everytime the Turn On button is pressed, the actionPerformed determines whether the server is already on or not, then if not runs 每次按下“打开”按钮,actionPerformed将确定服务器是否已经打开,如果没有,则运行

SwingWorkGUI swg = new SwingWorkGUI(); 
swg.execute();

The execution blocks in the following instruction, but only the second (third,ecc.) time it is called: 执行在以下指令中阻止,但只调用它的第二个(第三个,ecc。)时间:

mainServer = new Server();

Server.up is a static variable that helps me to kill the while(true) method in server. Server.up是一个静态变量,可以帮助我杀死服务器中的while(true)方法。

How can I kill the open instance without closing all the application? 如何在不关闭所有应用程序的情况下终止打开实例?

If I understand correctly your problem is that the ServerSocket object is blocked on the accept() call and you need to unblock it. 如果我理解正确你的问题是在accept()调用上阻塞了ServerSocket对象,你需要取消阻止它。 Some options: 一些选择:

  1. Call ss.close() from another thread (assumes ss is your ServerSocket object). 从另一个线程调用ss.close() (假设ss是您的ServerSocket对象)。 This will make accept() throw a SocketException . 这将使accept()抛出一个SocketException Reference: Javadocs for ServerSocket.close() 参考: ServerSocket.close() Javadocs

  2. Create a new Socket from another thread and connect to your own server. 从另一个线程创建一个新的Socket并连接到您自己的服务器。 This will unblock the accept() call. 这将取消阻止accept()调用。 Now you can check your Server.up flag. 现在,您可以检查Server.up标志。

  3. Set a timeout on your ServerSocket , eg 5 seconds. ServerSocket上设置超时,例如5秒。 This will make accept() throw an InterruptedIOException on timeout. 这将使accept()在超时时抛出InterruptedIOException Reference: Javadocs for SO_TIMEOUT 参考: 用于SO_TIMEOUT Javadocs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用另一个类的实例变量接受用户输入? - How do I accept user input with a instance variable of another class? 如何检查一个类的实例是否可以访问另一个类中的方法? - How do I check if an instance of a class has access to a method in another class? 如何使用此类创建实例? - how do I create an Instance with this class? 您如何从另一个类中调用另一个类中创建的类实例的方法? - How do you call the methods of a class instance that was created in another class, from another class? 如何从类的字符串表示形式转换为该类的实例? - How do I convert from the string representation of a class to an instance of the class? 如何在外部类构造函数中创建内部类的实例 - How do I create an instance of a inner class in the outer class constructor 如何将Groovy类的实例分配给Java类中的变量 - How do I assign instance of Groovy class to variable in Java class 如何创建另一个类的实例,如何向对象加载数据并打印出来? - How do I create an instance of another class, load an object with data and print it out? 如何在不创建新实例的情况下从另一个类访问变量 - How do I access a variable from another class without it creating a new instance 如何获取扩展 JPanel 以显示在另一个 JPanel 类中的类的实例? - How do I get an instance of a class that extends a JPanel to display in another classes JPanel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM