seleccionar paquete en Java

I am trying to re-write a Python library in Java. I was wondering if any java package was available which is analogous to the select module available in Python.

I have been referred to the nio package but I was wondering if there was a slighly more similar Java implementation.

preguntado el 08 de enero de 11 a las 20:01

Java doesn't have an equivalent of select() or poll(). Thus, you are stuck with NIO. -

Perhaps you could say what you are trying to do. There are loads of options/frameworks for doing this sort of thing. -

Java's threads are very light weight in Java 6. Often having one thread per source is the simplest way to process multiple streams. -

2 Respuestas

Not unless you are willing to write a JNI envoltorio para seleccione(2) yourself, or can find one on the 'net. (I looked briefly, and I didn't find one.) That's likely to be a fun little challenge, too, if you want to support Java's notion of an open file (e.g., to map a Java InputStream into a Unix file descriptor).

The preferred way to do this in Java is, as you've noted, with java.nio. nio does have some restrictions, though. The biggest difference between nio y seleccione(2), aside from the API differences, is that nio will only multiplex over network sockets. You can't use it to multiselect open files, for instance.

Respondido el 08 de enero de 11 a las 23:01

I would be very surprised if multi-selecting on files would be more efficient than using multi-threads to read one file each. - Peter Lawrey

That depends on the application. It also depends on how much multiselecting you have to do. If you have to handle a significant number of open sockets, you're going to run into problems with threads, for instance. Threads don't scale as well as a concurrent model that uses nio y java.util.concurrent. - Brian Clapper

Not sure if this will work for you, but you could try Jython instead of re-writing it in java.

Respondido el 08 de enero de 11 a las 23:01

This is a fair point. The Jython select module hews closely to the CPython seleccione module's interface, though it's based on Java channels, so it only works on network sockets, just like nio. - Brian Clapper

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.