PageRenderTime 5ms CodeModel.GetById 2ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/kilim/examples/SimpleTask.java

http://github.com/kilim/kilim
Java | 53 lines | 22 code | 7 blank | 24 comment | 2 complexity | 408da0b7646315cea2f077792b62116e MD5 | raw file
 1/* Copyright (c) 2006, Sriram Srinivasan
 2 *
 3 * You may distribute this software under the terms of the license 
 4 * specified in the file "License"
 5 */
 6
 7package kilim.examples;
 8
 9import kilim.Mailbox;
10import kilim.Pausable;
11import kilim.Task;
12
13/**
14 * Spawn a task, communicate through a shared mailbox. The task's
15 * termination is knowm through another mailbox.
16 * 
17 * The structure of this class is not much different from a Thread 
18 * version that uses PipedInput/OutputStreams (Task instead of Thread,
19 * execute() instead of run(), and typed, buffered mailboxes instead
20 * of pipes.
21 * 
22 * [compile] javac -d ./classes SimpleTask.java
23 * [weave]   java kilim.tools.Weave -d ./classes kilim.examples.SimpleTask
24 * [run]     java -cp ./classes:./classes:$CLASSPATH  kilim.examples.SimpleTask
25 */
26public class SimpleTask extends Task {
27    static Mailbox<String> mb = new Mailbox<String>();
28    
29    public static void main(String[] args) throws Exception {
30        new SimpleTask().start();
31        Thread.sleep(10);
32        mb.putnb("Hello ");
33        mb.putnb("World\n");
34        mb.putnb("done");
35    }
36
37    /**
38     * The entry point. mb.get() is a blocking call that yields
39     * the thread ("pausable")
40     */
41
42    public void execute() throws Pausable{
43        while (true) {
44            String s = mb.get();
45            if (s.equals("done")) break;
46            System.out.print(s);
47        }
48        
49        // This is not good form. Tasks shouldn't be exiting the system. 
50        // See SimpleTask2 for a better way to clean up.
51        System.exit(0);
52    }
53}