jsim.coroutine
Class Coroutine

java.lang.Object
  extended by jsim.coroutine.Coroutine
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
SimObject

public class Coroutine
extends java.lang.Object
implements java.lang.Runnable

This Coroutine class provides a more restricted form of interleaved execution than threads. In particular, only one coroutine can be in the running state at any one time. This reduces concurrency, but also simplifies synchronization problems. For certain domains such as simulation, this restricted concurrency is all that is needed. This coroutine package schedules couroutines to run in time order.

Version:
1.0
Author:
John Miller

Field Summary
 java.lang.Thread life
          Coroutine activity is via a thread.
 java.lang.String name
          Name of coroutine.
static int NORM_PRIORITY
          Normal priority level.
protected static java.util.logging.Logger trc
          Time ordered list of future coroutine activations.
 
Constructor Summary
Coroutine(java.lang.String name)
          Construct a coroutine.
Coroutine(java.lang.String name, double startTime)
          %%%%%%%%%%%%%%%%%%%%%% *** %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Constructor for testing only.
Coroutine(java.lang.String name, java.lang.Runnable runObj)
          Construct a coroutine.
 
Method Summary
static void DebugPQ()
           
 void end()
          Terminate the coroutine.
static double getTime()
          Get the current time (i.e., now).
 boolean initiateJoin(java.util.HashMap<java.lang.Integer,Coroutine> hmap, java.lang.Integer key)
          Remove from hashmap and put on activation list.
static boolean isTerminal()
          Determine whether all coroutines have terminated.
protected  void joinGroup(java.util.LinkedList<Coroutine> group)
          Join a special group of coroutines (e.g., those that actively move in an animation).
protected  Coroutine kickOut(java.util.LinkedList<Coroutine> queue)
          Remove from queue and put on activation list.
static void main(java.lang.String[] args)
          Main method for testing purposes only.
 void pause(double delay)
          Pause execution of coroutine for delay units of time.
 void preempt()
          Pause execution of coroutine for delay units of time.
 void run()
          The run method simulates a G/G/1 queue (example only).
 void setActTime(double coTime)
           
 void setState(int state)
          Sets the state of this coroutine
static void start()
          Transfer from main to first coroutine.
 java.lang.Object waitForJoin(java.util.HashMap<java.lang.Integer,Coroutine> hmap, java.lang.Integer key, Coroutine cor)
          y * Indefinate wait in a hashmap.
protected  void waitIn(java.util.LinkedList<Coroutine> queue)
          Indefinite wait in a queue.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NORM_PRIORITY

public static final int NORM_PRIORITY
Normal priority level.

See Also:
Constant Field Values

trc

protected static java.util.logging.Logger trc
Time ordered list of future coroutine activations.


life

public java.lang.Thread life
Coroutine activity is via a thread.


name

public final java.lang.String name
Name of coroutine.

Constructor Detail

Coroutine

public Coroutine(java.lang.String name,
                 java.lang.Runnable runObj)
Construct a coroutine.

Parameters:
runObj - Runnable object
name - Name of coroutine.

Coroutine

public Coroutine(java.lang.String name)
Construct a coroutine.

Parameters:
name - Name of coroutine.

Coroutine

public Coroutine(java.lang.String name,
                 double startTime)
%%%%%%%%%%%%%%%%%%%%%% *** %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Constructor for testing only.

Parameters:
name - Name of coroutine.
startTime - Starting time of coroutine.
Method Detail

setState

public void setState(int state)
Sets the state of this coroutine

Parameters:
state - the new state

joinGroup

protected void joinGroup(java.util.LinkedList<Coroutine> group)
Join a special group of coroutines (e.g., those that actively move in an animation). Subclasses not using the default (null) must invoke this method.

Parameters:
group - Outside list for group.

isTerminal

public static boolean isTerminal()
Determine whether all coroutines have terminated.

Returns:
boolean Whether no coroutines are left.

run

public void run()
The run method simulates a G/G/1 queue (example only). It should be overridden in extended classes.

Specified by:
run in interface java.lang.Runnable

getTime

public static double getTime()
Get the current time (i.e., now).

Returns:
double The time value of now.

waitIn

protected void waitIn(java.util.LinkedList<Coroutine> queue)
Indefinite wait in a queue.

Parameters:
queue - Waiting queue.

waitForJoin

public java.lang.Object waitForJoin(java.util.HashMap<java.lang.Integer,Coroutine> hmap,
                                    java.lang.Integer key,
                                    Coroutine cor)
y * Indefinate wait in a hashmap.

Parameters:
hMap - Waiting hashmap.
key - Key object for hashmap.
cor - Coroutine to wait in hashmap.

initiateJoin

public boolean initiateJoin(java.util.HashMap<java.lang.Integer,Coroutine> hmap,
                            java.lang.Integer key)
Remove from hashmap and put on activation list.

Parameters:
hmap - Waiting hashmap.
key - Key object for hashmap.

kickOut

protected Coroutine kickOut(java.util.LinkedList<Coroutine> queue)
Remove from queue and put on activation list.

Parameters:
queue - Waiting queue.
Returns:
Coroutine First on queue.

pause

public void pause(double delay)
Pause execution of coroutine for delay units of time.

Parameters:
delay - Delay time.

end

public void end()
Terminate the coroutine. The commented out contextSwitch (null, "end") allows garbage collection of completed entities but causes unusual glitches. Any suggestions?


preempt

public void preempt()
Pause execution of coroutine for delay units of time.


start

public static void start()
Transfer from main to first coroutine.


DebugPQ

public static void DebugPQ()

setActTime

public void setActTime(double coTime)

main

public static void main(java.lang.String[] args)
Main method for testing purposes only.

Parameters:
args - Command-line arguments.