当线程处于socket.receive(数据包)时中断线程;抛出一个错误

时间:2021-04-06 21:03:25

I'm developing an Android 3.1 Tablet application that runs a service with this thread:

我正在开发一个Android 3.1平板电脑应用程序,使用这个线程运行一个服务:

public class UDPServerThread extends Thread
{
    [ ... ]

    public void run()
    {
        while (!end)
        {
            try
            {
                byte[] buf = new byte[MESSAGE_SIZE];

                // Wait an incoming message.
                DatagramPacket packet = new DatagramPacket(buf, buf.length);
                socket.receive(packet);
                // TODO: Notify Service with packet received
                Message msg = new Message();
                msg.obj = packet;
                mServiceHandler.sendMessage(msg);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    public void stopServer()
    {
        Log.v("UDPServerThread", "stopServer");
        if (socket != null)
            socket.close();
        end = true;
    }
}

When I call stopServer() method from service I get the following error:

当我调用stopServer()方法时,我得到以下错误:

java.net.SocketException: Interrupted system call

Which is the right way to stop this thread while it is on socket.receive(packet);?

在这个线程位于socket.receive(数据包)上时,哪种方法是正确的?

2 个解决方案

#1


3  

The only real problem with this exception is that you cannot easily distinguish it from an actual I/O error. I'd suggest switching to using a SocketChannel rather than a Socket -- then you can interrupt your server thread and it will throw a ClosedByInterruptException instead, which you can catch and handle specifically.

这个异常唯一的真正问题是您不能轻易地将它与实际的I/O错误区分开来。我建议切换到使用SocketChannel而不是Socket——然后您可以中断服务器线程,它将抛出一个ClosedByInterruptException异常,您可以捕获并处理它。

#2


3  

According to the javadoc of the close method:

根据javadoc的close方法:

Any thread currently blocked in an I/O operation upon this socket will throw a SocketException.

在此套接字上的I/O操作中当前阻塞的任何线程都将抛出SocketException。

So when you call close, socket.receive(packet);, if it is currently in blocking mode, will throw a SocketException which is caught in your catch (IOException e) block and the stacktrace is printed.

因此,当您调用close时,socket.receive(packet);如果它当前处于阻塞模式,将抛出一个SocketException,并在catch (IOException e)块中捕获,并打印stacktrace。

#1


3  

The only real problem with this exception is that you cannot easily distinguish it from an actual I/O error. I'd suggest switching to using a SocketChannel rather than a Socket -- then you can interrupt your server thread and it will throw a ClosedByInterruptException instead, which you can catch and handle specifically.

这个异常唯一的真正问题是您不能轻易地将它与实际的I/O错误区分开来。我建议切换到使用SocketChannel而不是Socket——然后您可以中断服务器线程,它将抛出一个ClosedByInterruptException异常,您可以捕获并处理它。

#2


3  

According to the javadoc of the close method:

根据javadoc的close方法:

Any thread currently blocked in an I/O operation upon this socket will throw a SocketException.

在此套接字上的I/O操作中当前阻塞的任何线程都将抛出SocketException。

So when you call close, socket.receive(packet);, if it is currently in blocking mode, will throw a SocketException which is caught in your catch (IOException e) block and the stacktrace is printed.

因此,当您调用close时,socket.receive(packet);如果它当前处于阻塞模式,将抛出一个SocketException,并在catch (IOException e)块中捕获,并打印stacktrace。