CSP-style select as in Go using HawtDispatch

37 views
Skip to first unread message

Oliver Plow

unread,
Sep 3, 2013, 3:22:11 AM9/3/13
to hawtdi...@googlegroups.com
Hello,

I was thinking about adding CSP-style select as in Go to Java using HawtDispatch. A select in Go allows you to wait on several blocking channels till an item is added to any of the blocking channels. The select would then jump to the handler that serves the blocking channel an item was added to. See for example this sample snippet: https://gobyexample.com/select

When looking at HawtDispatch my first thought was that Dispatch Sources correspond to this idea, because when looking at http://hawtdispatch.fusesource.org/#NIO_Dispatch_Source you see that an event handler is registered with the queue (indirectly through a channel). But when looking at class SelectableChannel I see that they have tons of methods and I can't see the forest for the trees about how to use such a SelectableChannel in order to get a CSP-style select accomplished.

I think it wouldn't be hard to implement CSP-style select for HawtDispatch on my own. But I would first like to ask whether SelectableChannels can be used in some way to achieve this. Otherwise, it would be a fun little project to implement it for HawtDispatch.

Cheers, Oliver

Christian Posta

unread,
Sep 3, 2013, 9:38:45 AM9/3/13
to hawtdi...@googlegroups.com
You may wish to take a look at Custom Dispatch Sources with hawtdispatch:



--
You received this message because you are subscribed to the Google Groups "hawtdispatch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hawtdispatch...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Christian Posta
twitter: @christianposta

Oliver Plow

unread,
Nov 26, 2013, 4:48:13 AM11/26/13
to hawtdi...@googlegroups.com
Hello,

I assembled some few classes now to get this accomplished. It is here on GitHub. Some use cases are defined in the test class CoSelectTest. The simplest one looks like this:


CountDownLatch latch = new CountDownLatch(1);
CoSelect select = new CoSelect();
AtomicInteger count = new AtomicInteger(0);

Channel<String> channel = select.newChannel();
channel.onElementAdded((String str) -> {
    System.out.println(str);
    count.incrementAndGet();
    latch.countDown();
});

channel.add("hello world!");

latch.await();
Assert.assertEquals(count.get(), 1);

Something like CSP-style channels as in Go but the non-blocking way.
Reply all
Reply to author
Forward
0 new messages