PageRenderTime 27ms CodeModel.GetById 14ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/kilim/examples/SimpleTask2.java

http://github.com/kilim/kilim
Java | 49 lines | 26 code | 7 blank | 16 comment | 2 complexity | 37418701cefce4f8644e2bf97e3011a3 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.ExitMsg;
10import kilim.Mailbox;
11import kilim.Pausable;
12import kilim.Task;
13
14/**
15 * A slight extension to SimpleTask. This 
16 * 
17 * [compile] javac -d ./classes SimpleTask2.java
18 * [weave]   java kilim.tools.Weave -d ./classes kilim.examples.SimpleTask
19 * [run]     java -cp ./classes:./classes:$CLASSPATH  kilim.examples.SimpleTask2
20 */
21public class SimpleTask2 extends Task {
22    static Mailbox<String> mb = new Mailbox<String>();
23    static Mailbox<ExitMsg> exitmb = new Mailbox<ExitMsg>();
24    
25    public static void main(String[] args) throws Exception {
26        Task t = new SimpleTask2().start();
27        t.informOnExit(exitmb);
28        mb.putnb("Hello ");
29        mb.putnb("World\n");
30        mb.putnb("done");
31        
32        exitmb.getb();
33        System.exit(0);
34    }
35
36    /**
37     * The entry point. mb.get() is a blocking call that yields
38     * the thread ("pausable")
39     */
40
41    public void execute() throws Pausable{
42        while (true) {
43            String s = mb.get();
44            if (s.equals("done")) break;
45            System.out.print(s);
46        }
47        Task.exit(0); // Strictly optional.
48    }
49}