《图解java多线程设计模式》中的 ActiveObject 模式

16 篇文章 0 订阅

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();
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值