本文介绍了Java:如何在单个线程中与多个客户端进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个属于单个线程的套接字列表.但是我想知道是否有一种可行的方式与那些客户进行通讯(读/写).我不想为每个客户端创建一个线程,因为可能会有太多的用户,并且为每个客户端创建一个线程可能太昂贵.

I have a list of sockets that belongs to a single thread. But I wonder is there a feasible way to communicate (read/write from/to) with those those clients? I don't want to create one thread for each client because there may be too much users and creating one thread for each of them may be too costly.

推荐答案

我想说NIO是您最好的选择.查阅众多有关通过套接字进行NIO通信的好教程之一(嗯,SocketChannel)!

I'd say NIO is your best bet here. Check out one of the many good tutorials for NIO communications over a socket (well, SocketChannel)!

我认为这是我必须学习NIO时使用的教程: http://rox -xmlrpc.sourceforge.net/niotut/

I believe this is the tutorial that I used when I had to learn NIO: http://rox-xmlrpc.sourceforge.net/niotut/

这篇关于Java:如何在单个线程中与多个客户端进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 09:01