Channel Switch in GO, can I do this with java.nio

J

Jan Burse

Dear All,

I am just wondering whether a Go programming
language construct can be done in Java by
means of some standard packages.

The Go programming language construct, is the
switch statement over channels:

switch {
c <- channel1:
/* do something 1 */
c <- channel2:
/* do something 2 */
}

I interpret this that the code will try to fetch
data from channel1 and channel2, and depending on
which channel has fist some data, the corresponding
code will be executed?

How could this be done in Java? I have the feeling
that read() does not work since it blocks, and that
available() would not work since it would involve
polling. So something else could solve the problem...

Best Regards
 
M

markspace

I guess I will find code examples (to gradually help me
understanding the API) by myself via googling the net.
Any prefered site to start with?


Unfortunately I use hard copy for that. Learning Java, by O'Reilly.
Perhaps you can find an online copy.
 
J

Jan Burse

markspace said:
I like this article. It's old, but since NIO was new when the article
was written, it explains the basics without trying to do anything fancy.
It seem pretty close to the information I have in my book.

<https://www.ibm.com/developerworks/java/tutorials/j-nio/section9.html>

Thanks for the reference, very helpful.

P.S.:

Just out of habit I would code:

if ((key.readyOps() & SelectionKey.OP_ACCEPT)
== SelectionKey.OP_ACCEPT) {

// Accept the new connection
// ...
}

With a different check:

if ((key.readyOps() & SelectionKey.OP_ACCEPT)
!= 0) {

// Accept the new connection
// ...
}

The same check is also used in the definition
of the method isAcceptable(), so it could be
also coded as:

if (key.isAcceptable()) {

// Accept the new connection
// ...
}
 
J

Jan Burse

markspace said:
I like this article. It's old, but since NIO was new when the article
was written, it explains the basics without trying to do anything fancy.
It seem pretty close to the information I have in my book.

<https://www.ibm.com/developerworks/java/tutorials/j-nio/section9.html>

Something else: Some info around whether this NIO feature
is widely adopted? Anything known about the OS or hardware
support to implement this NIO feature in a JVM?

Further some info around about the origins of this construct,
I browsed over JSR-51 yesterday shortly, but the construct
was not clearly mentioned in the first proposal. Is this
some -ism from ADA or so?

Bye
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top