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

Discussion in 'Java' started by Jan Burse, Oct 17, 2011.

  1. Jan Burse

    Jan Burse Guest

    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
     
    Jan Burse, Oct 17, 2011
    #1
    1. Advertising

  2. Jan Burse

    markspace Guest

    On 10/17/2011 10:02 AM, Jan Burse wrote:

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



    <http://download.oracle.com/javase/7/docs/api/java/nio/channels/Selector.html>
     
    markspace, Oct 17, 2011
    #2
    1. Advertising

  3. Jan Burse

    Jan Burse Guest

    markspace schrieb:
    > On 10/17/2011 10:02 AM, Jan Burse wrote:
    >
    >> 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?

    >
    >
    > <http://download.oracle.com/javase/7/docs/api/java/nio/channels/Selector.html>
    >
    >
    >


    Thank you for the link.

    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?

    Best Regards
     
    Jan Burse, Oct 17, 2011
    #3
  4. Jan Burse

    markspace Guest

    On 10/17/2011 11:15 AM, Jan Burse wrote:
    >
    > 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.
     
    markspace, Oct 17, 2011
    #4
  5. Jan Burse

    Lew Guest

    markspace wrote:
    > Jan Burse wrote:
    >> 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.


    Developerworks has a number of useful articles.
    <https://www.ibm.com/search/csass/search/?sn=dw&en=utf&hpp=20&dws=dw&q=Java+NIO&Search=Search>

    --
    Lew
     
    Lew, Oct 17, 2011
    #5
  6. Jan Burse

    markspace Guest

    On 10/17/2011 3:58 PM, Lew wrote:
    > markspace wrote:
    >> Jan Burse wrote:
    >>> 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.

    >
    > Developerworks has a number of useful articles.
    > <https://www.ibm.com/search/csass/search/?sn=dw&en=utf&hpp=20&dws=dw&q=Java+NIO&Search=Search>
    >



    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>
     
    markspace, Oct 18, 2011
    #6
  7. Jan Burse

    Jan Burse Guest

    markspace schrieb:
    > 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
    // ...
    }
     
    Jan Burse, Oct 18, 2011
    #7
  8. Jan Burse

    Jan Burse Guest

    markspace schrieb:
    > 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
     
    Jan Burse, Oct 18, 2011
    #8
  9. Jan Burse

    markspace Guest

    On 10/18/2011 12:20 AM, Jan Burse wrote:
    > markspace schrieb:
    >> 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?



    <http://www.cl.cam.ac.uk/cgi-bin/manpage?2+select>

    My understanding is that it's critical to using Java in scalable
    applications like Tomcat. OS support precedes the adoption of Java's
    Select by a decade or more.
     
    markspace, Oct 18, 2011
    #9
  10. Jan Burse

    Jan Burse Guest

    markspace schrieb:

    > <http://www.cl.cam.ac.uk/cgi-bin/manpage?2+select>
    >
    > My understanding is that it's critical to using Java in scalable
    > applications like Tomcat. OS support precedes the adoption of Java's
    > Select by a decade or more.


    So this select was basically a unix port. Oki, Doki.
     
    Jan Burse, Oct 19, 2011
    #10
  11. Jan Burse

    Arne Vajhøj Guest

    On 10/18/2011 10:49 AM, markspace wrote:
    > On 10/18/2011 12:20 AM, Jan Burse wrote:
    >> markspace schrieb:
    >>> 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?

    >
    >
    > <http://www.cl.cam.ac.uk/cgi-bin/manpage?2+select>
    >
    > My understanding is that it's critical to using Java in scalable
    > applications like Tomcat. OS support precedes the adoption of Java's
    > Select by a decade or more.


    I believe Tomcat started supporting NIO in version 6 and
    I am not even sure it is default today.

    I consider earlier versions to be scaling OK.

    Arne
     
    Arne Vajhøj, Nov 6, 2011
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. iksrazal

    NIO with timeouts != NIO?

    iksrazal, Jun 17, 2004, in forum: Java
    Replies:
    1
    Views:
    6,334
    iksrazal
    Jun 18, 2004
  2. farseer
    Replies:
    2
    Views:
    474
    farseer
    Apr 11, 2005
  3. Replies:
    6
    Views:
    687
    Gordon Beaton
    Mar 17, 2008
  4. Replies:
    15
    Views:
    949
  5. Replies:
    6
    Views:
    399
Loading...

Share This Page