Re: [hawtdispatch] Using HawtDispatch to implement Go-style Goroutines

63 views
Skip to first unread message

Hiram Chirino

unread,
Jun 8, 2013, 10:05:30 AM6/8/13
to hawtdi...@googlegroups.com, hawtdi...@googlegroups.com
If you want to execute on a random thread of a fixed size thread pool, then execute the runnable on the global dispatch queue. I'd only use the serial dispatch queues when you need a serial execution of the runnable. 

Sent from my iPhone

On Jun 7, 2013, at 6:45 PM, jet...@web.de wrote:

Hello,

I'm thinking of making use of HawtDispatch to have something like goroutines as in Go: http://golang.org/doc/effective_go.html#goroutines HawtDispatch would do the thread pooling and the non-blocking dispatch which in Go is done by the runtime. In Go a goroutine is assigned directly to some thread. HawtDispatch has the additional indirection of going through queues. So my first-shot attempt would be to have a fixed size of DispatchQueues to which Runnables are added in a round-robin fashion every time in Java some Runnable needs to be executed asynchronously. This would look roughly like this:

public class Sample {

    private int numOfQueues = 5;
    private volatile DispatchQueue[] dispatchQueues = new DispatchQueue[numOfDispatchers];
    private BlockingQueue<Integer> availableQueues = new LinkedBlockingQueue<>();

    public Sample() {
          for(int i = 0; i < numOfQueues; i++) {
              dispatchQueues[i] = Dispatch.createQueue();
              availableQueues.add(i);
          }
    }

    public void async(Runnable runnable) throws InterruptedException {
        int nextQueue = availableQueues.take();
        dispatchQueues[nextQueue].execute(runnable);
        availableQueues.add(nextQueue);
    }

}

Now I wonder whether this approach is naive. What I don't like is the fixed amount of DispatchQueue, whether that will turn into a bottleneck. Maybe it is good enough for a start. Make the number of DispatchQueues grow dynamically if required. But what makes me think is that HawtDispatch also defines a fix number of threads if I got that right. Hm...

Some hints/ideas appreciated if time permits.
Thanks, Oliver

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

jet...@web.de

unread,
Jun 9, 2013, 3:29:27 AM6/9/13
to hawtdi...@googlegroups.com
Okay, implementing the Java pendant for the goroutine example in http://golang.org/doc/effective_go.html#goroutines here we go using JDK8:

public interface AsyncUtils {

    default public void async(Runnable runnable) {
        Dispatch.getGlobalQueue().execute(runnable);
    }

    default public void asyncAfter(long duration, TimeUnit unit, Runnable runnable) {
        Dispatch.getGlobalQueue().executeAfter(duration, unit, runnable);
    }

}

public class GoroutineTest implements AsyncUtils {

    private static Random Rand = new Random(System.currentTimeMillis());

    @After
    public void tearDown() throws InterruptedException
    {
        DispatcherConfig.getDefaultDispatcher().shutdown();
    }

    @Test
    public void asyncSort() throws InterruptedException
    {
        BlockingQueue<Integer> channel = new LinkedBlockingQueue<>();
        List<Integer> list = new ArrayList<>();
        for(int i = 0; i < 100; i++) {
            list.add(Rand.nextInt());
        }

        async(()-> {
            Collections.sort(list);
            channel.add(1);
        });

        int result = doSomethingForAWhile();
        int value = channel.take();
        Assert.assertEquals(1, value);
        Assert.assertTrue(result != -1);
    }

    private int doSomethingForAWhile() {
        int result = -1;
        while(!(Rand.nextInt(20) == 5) || result == -1) {
            result = Rand.nextInt();
        }
        return result;
    }
}

That was easy ... :-).

jet...@web.de

unread,
Jun 9, 2013, 9:07:28 AM6/9/13
to hawtdi...@googlegroups.com
Hello,

I sad down to write a little blog post titled "Go-style Goroutines in Java using HawtDispatch" (see http://objectscape.blogspot.de/2013/06/go-style-goroutines-in-java-using.html). If there is something in this post that is incorrect or you just don't like, please send mail to oliver [at] objectscape [dot] org and I will change it or remove it.

Cheers, Oliver

jet...@web.de

unread,
Jun 25, 2013, 9:31:24 AM6/25/13
to hawtdi...@googlegroups.com
Okay, it got accepted at java.dzone.com: http://java.dzone.com/articles/go-style-goroutines-java-and Maybe this helps to spread the news about HawtDispatch a bit. I really like how simple it is to use and how well it performs.

Christian Posta

unread,
Jun 25, 2013, 9:45:27 AM6/25/13
to hawtdi...@googlegroups.com
Already tweeted it! 


On Tuesday, June 25, 2013, wrote:
Okay, it got accepted at java.dzone.com: http://java.dzone.com/articles/go-style-goroutines-java-and Maybe this helps to spread the news about HawtDispatch a bit. I really like how simple it is to use and how well it performs.

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

Reply all
Reply to author
Forward
0 new messages