01: package dalma;
02:
03: import dalma.impl.FiberImpl;
04:
05: /**
06: * @author Kohsuke Kawaguchi
07: */
08: public abstract class Fiber<T extends Runnable> {
09: /**
10: * Gets the {@link Runnable} object that represents the entry point of this fiber.
11: *
12: * <p>
13: * The state of {@link Fiber} is not always kept in memory. For example,
14: * if a conversation is blocking on an event, its state is purged to the disk.
15: * Because of this, this method can be only invoked from other {@link Fiber}s
16: * in the same {@link Conversation}.
17: *
18: * @return
19: * never null. Always return the same {@link Runnable} object
20: * given to create a new {@link Fiber}.
21: *
22: * @throws IllegalStateException
23: * if this method is not invoked from a {@link Fiber} that belongs
24: * to the same {@link Conversation}.
25: */
26: public abstract T getRunnable();
27:
28: /**
29: * Starts executing this {@link Fiber}.
30: *
31: * This method works just like {@link Thread#start()}.
32: *
33: * @throws IllegalStateException
34: * if this fiber is already started.
35: */
36: public abstract void start();
37:
38: /**
39: * Waits until the execution of the fiber completes.
40: * This method works like {@link Thread#join()}
41: *
42: * <p>
43: * If called from another fiber, the calling fiber
44: * suspends and blocks until this fiber exits (and
45: * the thread will be reused to execute other fibers.)
46: *
47: * <p>
48: * If called from outside conversations, the calling
49: * method simply {@link #wait() waits}.
50: *
51: * @throws IllegalStateException
52: * If a fiber tries to join itself.
53: * @throws InterruptedException
54: * If this thread is interrupted while waiting.
55: */
56: public abstract void join() throws InterruptedException;
57:
58: /**
59: * Gets the current state of the fiber.
60: *
61: * @return never null
62: */
63: public abstract FiberState getState();
64:
65: /**
66: * Gets the {@link Conversation} that this fiber belongs to.
67: *
68: * A {@link Fiber} always belongs to the same {@link Conversation}.
69: *
70: * @return never null
71: */
72: public abstract Conversation getOwner();
73:
74: /**
75: * Creates a new {@link Fiber} within the current conversation.
76: */
77: public static <T extends Runnable> Fiber<T> create(T entryPoint) {
78: return FiberImpl.create(entryPoint);
79: }
80:
81: /**
82: * Returns the {@link Fiber} that the current thread is executing.
83: *
84: * <p>
85: * This mehtod can be only called from within the workflow.
86: *
87: * @throws IllegalStateException
88: * if the calling thread isn't a workflow thread.
89: */
90: public static Fiber<?> currentFiber() {
91: return FiberImpl.currentFiber(true);
92: }
93: }
|