Future.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package gnu.mapping;
  2. import gnu.kawa.io.InPort;
  3. import gnu.kawa.io.OutPort;
  4. public class Future<T> extends Thread implements Lazy<T>
  5. // FUTURE: implements java.util.concurrent.Future
  6. {
  7. public RunnableClosure<T> closure;
  8. public Future (Procedure action,
  9. InPort in, OutPort out, OutPort err)
  10. {
  11. closure = new RunnableClosure<T> (action, in, out, err);
  12. }
  13. public Future (Procedure action)
  14. {
  15. closure = new RunnableClosure<T>(action);
  16. }
  17. public static Future make (Procedure action, Environment penvironment,
  18. InPort in, OutPort out, OutPort err)
  19. {
  20. Environment saveEnv = Environment.setSaveCurrent(penvironment);
  21. try
  22. {
  23. return new Future(action, in, out, err);
  24. }
  25. finally
  26. {
  27. Environment.restoreCurrent(saveEnv);
  28. }
  29. }
  30. /** Get the CallContext we use for this Thread. */
  31. public final CallContext getCallContext() {
  32. return closure.getCallContext();
  33. }
  34. public void run() {
  35. closure.run();
  36. }
  37. public T getValue ()
  38. {
  39. try
  40. {
  41. join ();
  42. }
  43. catch (InterruptedException ex)
  44. {
  45. throw new RuntimeException ("thread join [force] was interrupted");
  46. }
  47. Throwable ex = closure.exception;
  48. if (ex != null)
  49. WrappedException.rethrow(ex);
  50. return closure.result;
  51. }
  52. public String toString() {
  53. StringBuffer buf = new StringBuffer();
  54. buf.append ("#<future ");
  55. buf.append(getName());
  56. buf.append(">");
  57. return buf.toString();
  58. }
  59. }