简体   繁体   English

如何阻止这种线程?

[英]How to stop this kind of thread?

I have one thread that has this code 我有一个具有此代码的线程

while(!this.isInterrupted()) {
    try {
        socket = server.accept();
    }
    catch(IOException e) {
        continue;
    }
}

When I call interrupt method from somewhere then it will stop executing only if it is checking condition. 当我从某个地方调用中断方法时,它只有在检查条件时才会停止执行。 What if the program is at server.accept() statement? 如果程序在server.accept()语句中怎么办? It will not stop till any request comes from any socket. 在任何套接字发出任何请求之前,它不会停止。 I want that when I call interrupt method this should stop immediately. 我希望当我调用中断方法时,应该立即停止。 Is there any solution for this problem. 有没有解决这个问题的方法。

Override the interrupt() method in your Thread like this: 像这样覆盖Thread中的interrupt()方法:

@Override
public void interrupt() {
    super.interrupt();
    socket.close();
}

When another thread interrupts this thread, the socket will get closed. 当另一个线程中断该线程时,套接字将被关闭。 When the thread is currently inside accept() , accept will exit immediately by throwing a SocketException . 当线程当前在accept() ,accept将通过抛出SocketException立即退出。 Your catch-block will catch that exception ( SocketException is a subclass of IOException ) and the check in the while-statement will be performed, which will then notice that the isInterrupted() flag is set and exit. 你的catch-block将捕获该异常( SocketExceptionIOException的子类)并且将执行while语句中的检查,然后将注意到isInterrupted()标志已设置并退出。

In contrary to the currently accepted solution it will exit immediately and not wait for up to 10 seconds to do so. 与目前接受的解决方案相反,它将立即退出,而不是等待长达10秒钟。

You could set a timeout on your ServerSocket: 您可以在ServerSocket上设置超时:

server.setSoTimeout(10000);

When you call the accept method now, it will accept new connections for 10 seconds, after that it throws a SocketTimeoutException. 当你现在调用accept方法时,它将接受10秒的新连接,然后抛出SocketTimeoutException。 Then you can do your accept again. 然后你可以再次接受。

Well, you could use the deprecated destroy() method to stop brutally murder the thread, but you really shouldn't do that because you don't know what it is doing at that moment. 好吧,你可以使用弃用的destroy()方法来 阻止 残酷谋杀线程,但是你真的不应该这样做,因为你不知道它在那一刻做了什么。 Chances are the accept-method is currently in the middle of some operation which must not be interrupted without cleanup. 有可能接受方法目前处于某些操作的中间,如果没有清理,就不能中断。

A better solution would be to call server.close() from another thread. 更好的解决方案是从另一个线程调用server.close() In that case the accept will exit by throwing a SocketException . 在这种情况下,accept将通过抛出SocketException退出。

To allow another thread to do this, you either need to expose a reference to your server-socket somehow, or you need to add a public method to your Thread-class which can be called from another thread and which then calls server.close() . 要允许另一个线程执行此操作,您需要以某种方式公开对服务器套接字的引用,或者您需要向Thread类添加一个公共方法,该方法可以从另一个线程调用,然后调用server.close() The best obtion depends on how the code around the snippet you posted looks. 最好的观点取决于您发布的代码段周围的代码。

You could extend ServerSocket and override the implAccept() method to instead have a timeout and check for the interrupt flag. 您可以扩展ServerSocket并覆盖implAccept()方法,而不是超时并检查中断标志。

Or you could create another thread that monitors the parent thread status and closes the socket when you interrupt it. 或者,您可以创建另一个监视父线程状态的线程,并在中断它时关闭套接字。

public void close()
           throws IOException
Closes this socket. Any thread currently blocked in accept() will throw a SocketException.
If this socket has an associated channel then the channel is closed as well.

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM