美文网首页
guarded-suspension模式

guarded-suspension模式

作者: jiahzhon | 来源:发表于2020-10-06 15:12 被阅读0次
  • guarded是“被保护着的”、“被防卫着的”意思。当现在并不适合马上执行某个操作时,就要求想要执行该操作的线程等待,以保障实例的安全性。(暂停的是服务器的端的线程)。

  • Request

public class Request {
    final private String value;

    public Request(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }
}
  • RequestQueue
public class RequestQueue {
    private  Queue<Request> queue = new LinkedList<>();
    public synchronized void putRequest(Request request){
        queue.offer(request);
        notifyAll();
    }
    public synchronized Request getRequest(){
        while (queue.peek() == null){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return queue.remove();
    }

}
  • ClientThread
public class ClientThread extends Thread {

    private final RequestQueue queue;

    private final Random random;

    private final String sendValue;

    public ClientThread(RequestQueue queue, String sendValue) {
        this.queue = queue;
        this.sendValue = sendValue;
        random = new Random(System.currentTimeMillis());
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("Client -> request " + sendValue);
            queue.putRequest(new Request(sendValue));
            try {
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
  • ServerThread
public class ServerThread extends Thread {

    private final RequestQueue queue;

    private final Random random;

    private volatile boolean closed = false;

    public ServerThread(RequestQueue queue) {
        this.queue = queue;
        random = new Random(System.currentTimeMillis());
    }

    /**
     * server三种被中断的方式:
     * 1:正要运行的时候发现closed标识已经为true了
     * 2:处于wait状态。closed起不到作用,调用close方法中断,request返回null
     * 3: sleep的时候被中断
     */
    @Override
    public void run() {
        while (!closed) {
            Request request = queue.getRequest();
            if (null == request) {
                System.out.println("Received the empty request.");
                continue;
            }
            System.out.println("Server ->" + request.getValue());
            try {
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {
                return;
            }
        }
    }

    public void close() {
        this.closed = true;
        this.interrupt();
    }
}
  • 调用类
public class SuspensionClient {
    public static void main(String[] args) throws InterruptedException {

        final RequestQueue queue = new RequestQueue();
        new ClientThread(queue, "Alex").start();
        ServerThread serverThread = new ServerThread(queue);
        serverThread.start();
        //serverThread.join();

        Thread.sleep(10000);
        serverThread.close();
    }
}

相关文章

  • guarded-suspension模式

    guarded是“被保护着的”、“被防卫着的”意思。当现在并不适合马上执行某个操作时,就要求想要执行该操作的线程等...

  • JS 设计模式

    工厂模式 单体模式 模块模式 代理模式 职责链模式 命令模式 模板方法模式 策略模式 发布-订阅模式 中介者模式 ...

  • iOS设计模式

    设计模式:MVC模式、单例模式、观察者模式、工厂模式、代理模式、策略模式、适配器模式、模版模式、外观模式、创建模式...

  • iOS知识点总结

    一、设计模式:MVC模式、单例模式、观察者模式、MVVM模式、工厂模式、代理模式、策略模式、适配器模式、模板模式、...

  • 常用设计模式 2018-09-15

    目录 代理模式 单例模式 命令模式 工厂模式 桥接模式 策略模式 装饰模式 观察者模式 门面模式 代理模式 静态代...

  • 计算机等级考试三级数据库复习(五)

    1.数据库系统 三级模式——》内模式,模式,外模式 二级映像模式(外模式/模式,模式/内模式) 模式/内模式提供数...

  • Retrofit源码解析

    设计模式:建造者模式、工厂方法模式、外观模式、代理模式、单例模式、策略模式、装饰模式、适配器模式、代理模式 一、R...

  • GOF23(java设计模式)--行为型模式

    职责链模式、迭代器模式、中介者模式、命令模式、解释器模式、访问者模式、策略模式、模版方法模式、状态模式、观察者模式...

  • vim学习 02——模式

    vim学习 02——模式 基本模式普通模式插入模式可视模式选择模式命令行模式Ex 模式 派生模式操作符等待模式插入...

  • 互联网营销与促销模式

    互联网营销与促销模式:众筹模式 团购模式 秒杀模式 满赠模式 积分模式 软文模式 会员模式 抽奖模式 体验模式 拍...

网友评论

      本文标题:guarded-suspension模式

      本文链接:https://www.haomeiwen.com/subject/bdstpktx.html