本文介绍了如何杀死正在等待Java中的函数调用的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个线程:

Thread t = new Thread(){
    public void run(){
        ServerSocketConnection scn = (ServerSocketConnection)
                Connector.open("socket://:1234");
        // Wait for a connection.
        SocketConnection sc = (SocketConnection) scn.acceptAndOpen();
       //do other operation
    }
};
t.start();

让我们说没有客户端正在连接到服务器,因此该线程将被阻止.现在我要杀死上述线程吗?我该如何杀死它?

Lets say no client is connecting to the Server, so this thread will be blocked.Now I want to kill the above thread t? How can I kill it?

推荐答案

Thread.interrupt()中断套接字上阻塞的线程.您可以尝试调用Thread.stop()Thread.destroy(),但是不建议使用这些方法(在J2ME中实际上是编辑 ),由于某些原因,您可以阅读此处.正如该文章所提到的,您所遇到的最佳解决方案是关闭您要阻塞的套接字:

Thread.interrupt() will not interrupt a thread blocked on a socket. You can try to call Thread.stop() or Thread.destroy(), but these methods are deprecated (edit: actually, absent in J2ME) and in some cases non-functional, for reasons you can read about here. As that article mentions, the best solution in your case is to close the socket that you're blocking on:

这篇关于如何杀死正在等待Java中的函数调用的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 07:00