/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. package kilim.examples;
  7. import kilim.Mailbox;
  8. import kilim.Pausable;
  9. import kilim.Task;
  10. /**
  11. * Spawn a task, communicate through a shared mailbox. The task's
  12. * termination is knowm through another mailbox.
  13. *
  14. * The structure of this class is not much different from a Thread
  15. * version that uses PipedInput/OutputStreams (Task instead of Thread,
  16. * execute() instead of run(), and typed, buffered mailboxes instead
  17. * of pipes.
  18. *
  19. * [compile] javac -d ./classes SimpleTask.java
  20. * [weave] java kilim.tools.Weave -d ./classes kilim.examples.SimpleTask
  21. * [run] java -cp ./classes:./classes:$CLASSPATH kilim.examples.SimpleTask
  22. */
  23. public class SimpleTask extends Task {
  24. static Mailbox<String> mb = new Mailbox<String>();
  25. public static void main(String[] args) throws Exception {
  26. new SimpleTask().start();
  27. Thread.sleep(10);
  28. mb.putnb("Hello ");
  29. mb.putnb("World\n");
  30. mb.putnb("done");
  31. }
  32. /**
  33. * The entry point. mb.get() is a blocking call that yields
  34. * the thread ("pausable")
  35. */
  36. public void execute() throws Pausable{
  37. while (true) {
  38. String s = mb.get();
  39. if (s.equals("done")) break;
  40. System.out.print(s);
  41. }
  42. // This is not good form. Tasks shouldn't be exiting the system.
  43. // See SimpleTask2 for a better way to clean up.
  44. System.exit(0);
  45. }
  46. }