[英]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: 一些选择:
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
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
标志。
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.