Lasitha Yapa
Lasitha Yapa

Reputation: 4609

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)

Upvotes: 1

Views: 1301

Answers (2)

Kayaman
Kayaman

Reputation: 73558

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).

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.

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. 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.

If you do want to spend time with NIO, there are plenty of questions on SO that discuss it like Java NIO Server

Upvotes: 3

Emre Acar
Emre Acar

Reputation: 920

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.

Upvotes: 0

Related Questions