java 如何在线程中停止块方法 DatagramSocket.receive()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7962121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can i stop the block method DatagramSocket.receive() in a thread
提问by cloud
i create a DatagramSocket
in the main thread,and then create a inner class thread to listen the port. when i close the DatagramSocket
in the main thread, it always came across an error socket closed
because in the inner class thread i called the receive()
method,and it blocked the inner class thread. here is the code of the inner class:
我DatagramSocket
在主线程中创建一个,然后创建一个内部类线程来监听端口。当我关闭DatagramSocket
主线程中的 时,它总是遇到错误,socket closed
因为在内部类线程中我调用了该receive()
方法,并且它阻塞了内部类线程。这是内部类的代码:
class ReceiveText extends Thread
{
@Override
public void run()
{
while(true)
{
byte[] buffer = new byte[1024];
DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
try {
udpSocket.receive(dp);//blocked here
byte[] data = dp.getData();
String message = new String(data, 0 , dp.getLength());
setTxtAreaShowMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
i want to stop the inner class thread before close the DatagramSocket, but the stop()
method is not recommended. how can i do that?
我想在关闭DatagramSocket之前停止内部类线程,但stop()
不推荐该方法。我怎样才能做到这一点?
回答by prunge
Close the socket, which will stop the receive() call from blocking. If you first set a closed flag then in the catch (IOException) block you can safely ignore the exception if the flag is set. (You could probably also use isClosed() method on DatagramSocket instead of a flag)
关闭套接字,这将阻止接收()调用阻塞。如果您首先设置了一个关闭标志,那么在 catch (IOException) 块中,如果设置了该标志,您可以安全地忽略该异常。(您也可以在 DatagramSocket 上使用 isClosed() 方法而不是标志)
回答by Cheng
Socket.close() does the trick. Or you can use socket.setSoTimeout(1000); the setSoTimeout() method allows you to define a timeout period in milliseconds. For example:
Socket.close() 可以解决问题。或者你可以使用 socket.setSoTimeout(1000); setSoTimeout() 方法允许您以毫秒为单位定义超时时间。例如:
//if the socket does not receive anything in 1 second,
//it will timeout and throw a SocketTimeoutException
//you can catch the exception if you need to log, or you can ignore it
socket.setSoTimeout(1000);
socket.receive();
Here is the javadoc for setSoTimeout();
By default, the timeout is 0 which is indefinite, by changing it to a positive number, it will only block for the amount you specified. (Make sure you set it before calling socket.receive())
默认情况下,超时为 0,这是不确定的,通过将其更改为正数,它只会阻止您指定的数量。(确保在调用 socket.receive() 之前设置它)
Here is an example answered on this site: set timeout for socket receive
这是此站点上回答的示例: set timeout for socket receive
回答by Martin James
With UDP, you could send the socket a datagram from another thread, so unblocking the read(). The datagran could, (depending on your protocol), contain a 'suicide' command or you could use an additional 'shutdown' boolean that the thread reads after read() returns.
使用 UDP,您可以从另一个线程向套接字发送数据报,从而解除对 read() 的阻塞。datagran 可以(取决于您的协议)包含“自杀”命令,或者您可以使用线程在 read() 返回后读取的附加“关闭”布尔值。