All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kilim.TaskGroup Maven / Gradle / Ivy

Go to download

Coroutines, continuations, fibers, actors and message passing for the JVM

There is a newer version: 2.0.2-jdk7
Show newest version
// Copyright 2006 by sriram - offered under the terms of the MIT License

package kilim;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

public class TaskGroup extends Task {
    private Mailbox addedTasksMB = new Mailbox();
    private Mailbox exitmb = new Mailbox();
    private HashSet tasks = new HashSet();
    
    public List results = Collections.synchronizedList(new ArrayList());

    public void execute() throws Pausable {
        while (!tasks.isEmpty() || addedTasksMB.hasMessage()) {
            switch (Mailbox.select(addedTasksMB, exitmb)) {
            case 0: 
                Task t = addedTasksMB.getnb();
                t.informOnExit(exitmb);
                tasks.add(t);
                break;
            case 1: 
                ExitMsg em = exitmb.getnb();
                results.add(em);
                tasks.remove(em.task);
                break;
            }
        }
        exit(results);
    }
    
    @Override
    public ExitMsg joinb() {
        start();
        return super.joinb();
    }
    
    @Override
    public ExitMsg join() throws Pausable {
        start();
        return super.join();
    }

    public void add(Task t) {
        t.informOnExit(exitmb);
        addedTasksMB.putnb(t); // will wake up join if it is waiting.
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy