KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > emn > info > eaop > aspect > Coroutining


1 /* ----------------------------------------------------------------------------
2  * EAOP 1.0, 2002-12-19
3  * (c) 2002 Remi Douence, Mario Sudholt; OBASCO group; EMN/INRIA; France
4  * THIS SOFTWARE IS PROVIDED AS IS AND WITHOUT ANY WARRANTY
5   -------------------------------------------------------------------------- */

6
7 package fr.emn.info.eaop.aspect;
8
9 import fr.emn.info.eaop.Monitor;
10
11 import java.util.*;
12 import java.lang.reflect.*;
13 import java.io.*;
14
15 /**
16  * This class offers coroutining behavior: when an event is generated
17  * by execution, the monitor is called. The monitor resume every
18  * pending aspects, one aspect at a time. Aspects block on (i.e wait)
19  * next event. All aspects share the same semaphore.
20  *
21  * @author RD
22  * @version 1.0
23  */

24 public abstract class Coroutining extends AspectS implements Runnable JavaDoc {
25
26     Thread JavaDoc thread;
27
28     public Coroutining(AspectS up) {
29     super(up);
30         this.thread = new Thread JavaDoc(this);
31     synchronized (this) {
32         this.thread.start();
33         try {
34         this.wait();
35         } catch (Exception JavaDoc e) {
36         System.out.println("bug in the coroutining code");
37         e.printStackTrace();
38         System.exit(0);
39         }
40     }
41     }
42
43     /**
44      * Start the aspect thread.
45      */

46     public void run() {
47     this.definition();
48     // this should be removed from the tree here
49
this.done();
50     }
51
52     /**
53      * this method is called by the caller (monitor or aspect) in
54      * order to resume the current aspect (this)
55      */

56     public void go() {
57     Monitor.monitor.path.push(this);
58     this.swap();
59     Monitor.monitor.path.pop();
60     }
61     /**
62      * this method is called by the current aspect (this) in order to
63      * pause and resume the caller (monitor or aspect)
64      */

65     public void block() { // called by the callee
66
this.swap();
67     }
68
69     /**
70      * this method pass control from one thread to the other one
71      * waiting on this
72      */

73     void swap() {
74     synchronized (this) {
75         this.notify();
76         try {
77         this.wait();
78         } catch (Exception JavaDoc e) {
79         System.out.println("bug in the coroutining code");
80         e.printStackTrace();
81         System.exit(0);
82         }
83     }
84     }
85
86     /**
87      * this method is called at the end of an aspect definition in
88      * order to resume the caller (and the aspect (this) does not
89      * block but it terminates (it sohuld remove itself from the tree
90      * of aspects of the monitor)
91      */

92     public void done() {
93     synchronized (this) {
94         this.notify();
95     }
96     }
97
98    /**
99      * Main method (i.e., entry point) of a coroutine (an aspect), to
100      * be redefined by subclasses
101      */

102     abstract public void definition();
103 }
104
Popular Tags