Task.spawn

27 views
Skip to first unread message

kilim

unread,
Aug 26, 2014, 12:21:18 PM8/26/14
to kilimt...@googlegroups.com
I added a static called Task.spawn() to the asm5-jdk8 branch. Now you don't have to create Task classes and instances of your own. Simply pass in a lambda; it is allowed to make pausable calls.
Task.spawn returns the spawned (and started) task.

--sriram

Example:

package kilim.examples;


import kilim.Mailbox;

import kilim.Task;


/**

 * Spawn example with one consumer, ten producers

 */

public class  Spawn {

    public static void main(String[] args) throws Exception {

        // mb is captured by all lambdas.

        Mailbox<String> mb = new Mailbox<String>();

        

        //Consumer

        Task.spawn( () -> {

            while (true) {

                String s = mb.get();  // mb captured from environment.

                System.out.println(s);

            }

        });

        // Producers

        for (int i = 0; i < 10; i++) {

            final int fi = i; // Need a 'final' i to pass to closure

            Task.spawn( () -> {

                mb.put("Hello from " + fi);  // mb and fi captured from environment

            });

        }

    }

}


Reply all
Reply to author
Forward
0 new messages