Python's select.select equivalent in Java

Question:

I’ve been socket programming in Python for an assignment using select.select(rfile, wfile, xlist[, timeout]) to attend to requests made by a list of client sockets that are connected, and I also want to program in Java, and was looking for an equivalent to this select class that I could use in Java, but I couldn’t find any and so I’ve decided to ask here. Can somebody tell me an equivalent or something similar that I could use?

Asked By: hwkd

||

Answers:

As it written here select() returns three new lists, containing subsets of the contents of the lists passed in. readable list, writable list, exceptional list. Java doesn’t have multiply return and parallel assignment, basically this is an answer.

Java doesn’t have such amount of high-level build-ins as Python does. In every Python module we can find some opportunities that will take hundreds of lines in Java to repeat.

Answered By: Rudziankoŭ

you can get select()-like behaviour by using Java NIO Channels… Java Implements Selector class, and channels (SelectableChannels) suscribe to it. A Channel can be a SocketChannel or another stream-like object that extends SelectableChannel Class.

Just like python’s select(), Selector will use operating system’s io interfaces in order to monitor streams/file descriptors.

A gentle introduction can be found here:
https://www.baeldung.com/java-nio-selector

Answered By: RAngeles
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.