Java异步网络 [英] Async networking in java

查看:73
本文介绍了Java异步网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中执行非阻塞套接字的最佳解决方案是什么? Socket类本身不支持此功能.我知道NIO,但听说过传闻说不是很好,或者使用起来很棘手?
我找到了Apache的MINA( http://mina.apache.org [ ^ ]))看起来很有希望,但想知道这里的人使用了什么.

What''s the best solution for doing non blocking sockets in Java? The Socket class in and of itself doesn''t support this. I''m aware of NIO but have heard rumors that''s not very good or perhaps tricky to use?
I found Apache''s MINA (http://mina.apache.org[^]) which looks promising, but wondered what people here have used.

推荐答案

Jim,最简单的方法是创建ServerSocket侦听其自己的线程. 侦听线程被阻止,但是您仍然可以调用close()方法.
这将导致SocketException抛出在侦听线程上,以允许退出即可.

请参见 Class:ServerSocket [^ ].

Jim,
the easiest is to create the ServerSocket listening on it''s own thread. The listening thread is blocked, but you can still call the close() method.
This will cause a SocketException to be thrown on the listening thread to allow for graceful exit.

See Class:ServerSocket[^] for details.


这篇关于Java异步网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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