English 中文(简体)
间断线的read线,被阻挡在海卫组织上,从车上等待吗?
原标题:Interrupting thread which is blocked waiting on IO from socket?

我有一封信,聆听了特定东道国名称和港口的连接。 听取意见时使用以下方法:listen()(见下文),该方法不断等待使用<代码>ServerSocket.accept()的新链接,创建新的<代码>Thread,处理投入流。

private ServerSocket serverSocket;
private Thread listenerThread;

public void listen() throws IOException {
    this.listenerThread = new Thread(new Runnable() {

        @Override
        public void run() {
            while (true) {
                try {
                    Socket socket = TheServerClass.this.serverSocket.accept();
                    // Create new thread to handle the incoming connection
                }
                catch (IOException exc) { }
            }
        }
    });
    this.listenerThread.start();
}

Now I want to stop the running of listenerThread. But when I call this.listenerThread.interrupt(), this doesn t work.
I thought you can stop a thread by interrupting it, so why isn t that working?

(Notice: 一种可能的解决办法是使用<代码>ServerSocket 关闭 .serverSocket.close(),但可以用interrupt(或某种方式完成?)

最佳回答

Call serverSocket.close(),

我猜测,因为你没有做综合研究,你无法打断,而且由于<代码>accept() , t。 中断的欺骗,使你打断。 胎面被打断,但你必须检查一下<条码>。

。 我如何打断服务器接收方法?

问题回答

答案是个问题。 你们需要结束工作。 它使用<代码>serverSocket.close(>.Thread.interrupt(<>/code>)对袖珍进行跟踪。

Use this:

public class MyThread extends Thread {

    private boolean stop;
    private ServerSocket serverSocket;

    public MyThread(ServerSocket ss) {
        this.serverSocket = ss;
        this.stop = false;
    }

    public void setStop() {
        this.stop = true;
        if (this.ss != null) {
            this.ss.close();
        }
    }

    public void run() {
        while (!stop) {
            try {
                Socket socket = serverSocket.accept();
                // Create new thread to handle the incoming connection 
            }
            catch (IOException exc) { }
        }
    }
}

http://www.un.org/Depts/DGACM/index_french.htm





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...