简体   繁体   English

不使用线程的Java套接字服务器

[英]Java socket server without using threads

I am working on a application which should be very light weight and use minimum number of threads. 我正在开发一个重量很轻并且使用最少线程数的应用程序。
I need a socket server in my application for heartbeat monitor. 我的应用程序中需要一个用于心跳监视器的套接字服务器。

ServerSocket listener= new ServerSocket(port);
while (true) {
   Socket socket = listener.accept();

   Runnable thread = new HBClient(this, socket);
   thread.run();
}

Problem here is I have to use one thread per one client. 这里的问题是我必须为每个客户端使用一个线程。
Is there a way to do this without using threads? 有没有一种方法可以不使用线程? Maybe an event driven approach to identify when a client is connected or a non blocking method to accept clients.(I already checked java.nio but it seems even that cannot be used without threads) 也许是一种事件驱动的方法来确定客户端何时连接,或者是一种非阻塞方法来接受客户端。(我已经检查过java.nio,但似乎没有线程也无法使用)

Using NIO (for New IO, not Non-blocking IO) you can use a Selector on a single thread to handle multiple channels whereas with basic IO you have one thread responsible for one task (accepting connections or doing communication on a connection). 使用NIO(对于新IO,不是非阻塞IO),您可以在单个线程上使用Selector来处理多个通道,而对于基本IO,您可以让一个线程负责一项任务(接受连接或在连接上进行通信)。

The basic premise is that you have resources and the single selector will "spin around" and choose one of them to process for whatever needs to be done (connect, read, write). 基本前提是您拥有资源,并且单个选择器将“旋转”并选择其中一个来处理需要完成的任何事情(连接,读取,写入)。 Once that's done, another resource will be selected and so on. 完成后,将选择另一个资源,依此类推。 Of course a resource won't be selected unless there's actually something to do, and the channels inform that with SelectionKey flags to indicate which operations can be done. 当然,除非实际要做某事,否则不会选择资源,并且通道会使用SelectionKey标志来通知该资源,以指示可以执行哪些操作。

However using non-blocking IO is a lot harder to program to than basic IO, and if you're not handling a lot of resources it won't be that much of an [improvement]( NIO Performance Improvement compared to traditional IO in Java ) either. 但是,与基本IO相比,使用无阻塞IO 编程起来困难得多,并且如果您不处理大量资源,则与Java中的传统IO相比,NIO性能不会有太大的[改进] )。 Even if you do want to use NIO it's recommended that unless you do NIO for learning purposes, use an existing framework like Netty that will make it a lot easier for you to concentrate on the functionality of the program and not the intricacies of getting NIO to work properly. 即使您确实想使用NIO,也建议您除非您出于学习目的而使用NIO,否则请使用现有的框架(如Netty),这将使您更轻松地专注于程序的功能,而不是使NIO复杂的问题。好好工作。

If you do want to spend time with NIO, there are plenty of questions on SO that discuss it like Java NIO Server 如果您确实想花时间与NIO交流,那么SO上有很多问题需要讨论,例如Java NIO Server。

No. Even if you try to implement an event driven approach, someone should still listen to the socket to throw an event. 不会。即使您尝试实现事件驱动的方法,也应该有人监听套接字以引发事件。 So it is basically impossible to do this with a single thread. 因此,基本上不可能使用单个线程来执行此操作。

But, you can break the infinite loop when you notify a connected client. 但是,当您通知连接的客户端时,您可以打破无限循环。 You won't be accepting new clients but you'll be in a single thread. 您不会接受新客户,但是您将处于一个单一线程中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM