ActivationbQueue
public class ActivationbQueue {
private static final int max_method_request = 100 ;
private final MethodRequest [ ] requestsQueue;
private int tail;
private int head;
private int count;
public ActivationbQueue ( ) {
this . requestsQueue = new MethodRequest [ max_method_request] ;
this . head = 0 ;
this . tail = 0 ;
this . count = 0 ;
}
public synchronized void putRequest ( MethodRequest request) {
while ( count >= requestsQueue. length) {
try {
wait ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
requestsQueue[ tail] = request;
tail = ( tail + 1 ) % requestsQueue. length;
count++ ;
notifyAll ( ) ;
}
public synchronized MethodRequest takeRequest ( ) {
while ( count <= 0 ) {
try {
wait ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
MethodRequest request = requestsQueue[ head] ;
head = ( head + 1 ) % requestsQueue. length;
count-- ;
notifyAll ( ) ;
return request;
}
}
ActiveObject
public interface ActiveObject {
public abstract Result < String > makeString ( int count, char fillchar) ;
public abstract void displayString ( String name) ;
}
ActiveObjectFactory
public class ActiveObjectFactory {
public static ActiveObject createActiveObject ( ) {
Servant servant = new Servant ( ) ;
ActivationbQueue activationbQueue = new ActivationbQueue ( ) ;
SchedulerThread schedulerThread = new SchedulerThread ( activationbQueue) ;
Proxy proxy = new Proxy ( schedulerThread, servant) ;
schedulerThread. start ( ) ;
return proxy;
}
}
DisplayClientThread
public class DisplayClientThread extends Thread {
private final ActiveObject activeObject;
public DisplayClientThread ( String name, ActiveObject activeObject) {
super ( name) ;
this . activeObject = activeObject;
}
public void run ( ) {
try {
for ( int i = 0 ; true ; i++ ) {
String string = Thread . currentThread ( ) . getName ( ) + " " + i;
activeObject. displayString ( string) ;
Thread . sleep ( 200 ) ;
}
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
DisplayStringRequest
public class DisplayStringRequest extends MethodRequest < Object > {
private final String string;
public DisplayStringRequest ( Servant servant, String string) {
super ( servant, null ) ;
this . string = string;
}
public void execute ( ) {
servant. displayString ( string) ;
}
}
FutureResult
public class FutureResult < T > extends Result < T > {
private Result < T > result;
private boolean ready = false ;
public synchronized void setResult ( Result < T > result) {
this . result = result;
this . ready = true ;
notifyAll ( ) ;
}
public synchronized T getResultValue ( ) {
while ( ! ready) {
try {
wait ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
return result. getResultValue ( ) ;
}
}
Main
public class Main {
public static void main ( String [ ] args) {
ActiveObject activeObject = new ActiveObjectFactory ( ) . createActiveObject ( ) ;
new MakerClientThread ( "Alice" , activeObject) . start ( ) ;
new MakerClientThread ( "bobby" , activeObject) . start ( ) ;
new DisplayClientThread ( "chris" , activeObject) . start ( ) ;
}
}
MakerClientThread
public class MakerClientThread extends Thread {
private final ActiveObject activationbQueue;
private final char fillerchar;
public MakerClientThread ( String name, ActiveObject activeObject) {
super ( name) ;
this . activationbQueue = activeObject;
this . fillerchar = name. charAt ( 0 ) ;
}
public void run ( ) {
try {
for ( int i = 0 ; true ; i++ ) {
Result < String > result = activationbQueue. makeString ( i, fillerchar) ;
Thread . sleep ( 10 ) ;
String value = result. getResultValue ( ) ;
System . out. println ( Thread . currentThread ( ) . getName ( ) + " : value =" + value) ;
}
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
MakeStringRequest
public class MakeStringRequest extends MethodRequest < String > {
private final int count;
private final char fillchar;
public MakeStringRequest ( Servant servant, FutureResult < String > futureResult, int count, char fillchar) {
super ( servant, futureResult) ;
this . count = count;
this . fillchar = fillchar;
}
public void execute ( ) {
Result < String > result = servant. makeString ( count, fillchar) ;
future. setResult ( result) ;
}
}
MethodRequest
abstract class MethodRequest < T > {
protected final Servant servant;
protected final FutureResult < T > future;
protected MethodRequest ( Servant servant, FutureResult < T > futureResult) {
this . servant = servant;
this . future = futureResult;
}
public abstract void execute ( ) ;
}
Proxy
public class Proxy implements ActiveObject {
private final SchedulerThread schedulerThread;
private final Servant servant;
public Proxy ( SchedulerThread schedulerThread , Servant servant) {
this . schedulerThread = schedulerThread;
this . servant = servant;
}
public Result < String > makeString ( int count, char fillchar) {
FutureResult < String > futureResult = new FutureResult ( ) ;
schedulerThread. invoke ( new MakeStringRequest ( servant, futureResult, count, fillchar) ) ;
return futureResult;
}
public void displayString ( String string) {
schedulerThread. invoke ( new DisplayStringRequest ( servant, string) ) ;
}
}
RealResult
public class RealResult < T > extends Result < T > {
private final T resultValue;
public RealResult ( T resultValue) {
this . resultValue = resultValue;
}
public T getResultValue ( ) {
return resultValue;
}
}
Result
public abstract class Result < T > {
public abstract T getResultValue ( ) ;
}
SchedulerThread
public class SchedulerThread extends Thread {
private final ActivationbQueue queue;
public SchedulerThread ( ActivationbQueue queue) {
this . queue = queue;
}
public void invoke ( MethodRequest request) {
queue. putRequest ( request) ;
}
public void run ( ) {
while ( true ) {
MethodRequest request = queue. takeRequest ( ) ;
request. execute ( ) ;
}
}
}
Servant
public class Servant implements ActiveObject {
@Override
public Result < String > makeString ( int count, char fillchar) {
char [ ] buffer = new char [ count] ;
for ( int i = 0 ; i < count; i++ ) {
buffer[ i] = fillchar;
try {
Thread . sleep ( 100 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
return new RealResult < String > ( new String ( buffer) ) ;
}
public void displayString ( String string) {
try {
System . out. println ( "displayString " + string) ;
Thread . sleep ( 10 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}