有没有腾出一个阻塞套接字上一个线程没有关闭套接字的方式? [英] Is there a way to free up a thread blocked on a socket without closing the socket?

查看:136
本文介绍了有没有腾出一个阻塞套接字上一个线程没有关闭套接字的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个线程,不断阻挡插座上,一切都在读书,然后再次阻塞,等待更多。它负责获取响应,交给他们去各个线程/窗户发出的请求的异步通信协议的一部分。它也处理应对还活着?超时消息,等等等等,我正在寻找一种方法来关闭这个循环有一个方法调用,基本上腾出一些人工干预插座,以防我永远需要它。

I have a thread that is constantly blocking on a socket, reading in everything, then blocking again, waiting for more. It's part of an asynchronous communication protocol responsible for getting responses and handing them out to the various threads/windows that made the requests. It also handles responding to "still alive?," time out messages, etc etc. What I'm looking for is a way to shut down this loop with a method call, basically to free up the socket for some manual intervention, in case I ever need it.

我曾经的方式告知取消阻止的套接字上等待是有别的调用关闭套接字上一个线程,虽然我不不想这样做,我宁愿把它打开。我已经在过去处理了这个Kludgier办法是要么关闭阻塞在插座上,只是有它睡的时间间隔,或者设置一个套接字超时继续循环通过,但两者那些轮询的一种形式,这是我希望避免的。

The way I was once told to unblock a thread that's waiting on a socket is to have something else call Close on the socket, though I don't want to do that, I'd rather keep it open. Kludgier ways I've handled this in the past was to either turn off blocking on the socket, and just have it sleep for intervals, or set a socket timeout to keep looping through, but both of those are a form of polling, something I want to avoid.

也许有一些方法,我可以使用事件做到这一点,有没有当插座有一些数据我抛出一个事件?

Perhaps there's some way I could do it using events, is there an event thrown when the socket has some data for me?

推荐答案

您可以使用了Thread.interrupt - 阻塞调用将需要被处理的ThreadInterruptedException返回

You can use Thread.Interrupt - the blocking call will return with an ThreadInterruptedException which needs to be handled.

您可以找到一个例子这里

这篇关于有没有腾出一个阻塞套接字上一个线程没有关闭套接字的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆