一天的事情---GuardSuspension模式

1,什么是GuardSSuspension模式

这是一种队列模式,即当前事情还未处理完成,但有新的事情发生时,先将刚发生的事情,放入一个队列中,手头上的事情处理完成后,再来处理新的事情。

2,好处,

具有一定的顺序,先请求,先处理。生活就会比较有规律。

3,举例

a)请求

public class Request {

    private String value ;

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

    public String getValue(){
        return this.value;
    }
}

b)请求队列

import java.util.LinkedList;

/**
 * 大脑相当于一个请求队列,用于接收请求,传递请求
 */
public class BrainQueue {

    private final LinkedList<Request> queue = new LinkedList<>();

    public Request getRequest(){
        synchronized (queue){
            while (queue.size() <= 0){
                try {
                    this.queue.wait();
                } catch (InterruptedException e) {
                    return null;
                }
            }

            return queue.removeFirst();
        }
    }

    public void putRequest(Request request){
        synchronized (queue){
            this.queue.addLast(request);
            this.queue.notifyAll();
        }
    }

}

c)外部事件

import java.util.Random;

/**
 * 外部事件
 */
public class OutEventThread extends Thread {

    private final BrainQueue queue;

    private final Random random;

    private final String eventName;

    public OutEventThread(BrainQueue queue,String eventName){
        this.queue = queue;
        this.eventName = eventName;
        this.random = new Random(System.currentTimeMillis());
    }

    @Override
    public void run() {
        queue.putRequest(new Request(eventName));
        System.out.println("接收到外部请求事件:"+eventName);
        System.out.println("让我喘口气");
        try {
            Thread.sleep(random.nextInt(1000));
        } catch (InterruptedException e) {
            return;
        }
    }
}

d)身体处理

import java.util.Random;

/**
 * 身体开始处理实行
 */
public class BodyThread extends Thread {

    private final BrainQueue queue;

    private final Random random;

    private volatile boolean close = false;

    public BodyThread(BrainQueue queue){
        this.queue = queue;
        this.random = new Random(System.currentTimeMillis());
    }

    @Override
    public void run() {
        while (!close){
            Request request = queue.getRequest();
            if(null == request){
                System.out.println("事情终于做完了,累死了");
                continue;
            }
            System.out.println("身体开始处理"+ request.getValue()+"事件");
            try {
                Thread.sleep(random.nextInt(1000));
                System.out.println(request.getValue()+"处理完成");
            } catch (InterruptedException e) {
                return;
            }

        }
    }

    public void close(){
        this.close = true;
        this.interrupt();
    }
}

e)测试类

public class OneDayTest {

    public static void main(String[] args) throws InterruptedException {
        BrainQueue queue = new BrainQueue();

        new OutEventThread(queue,"睁眼").start();
        BodyThread body = new BodyThread(queue);
        body.start();
        new OutEventThread(queue,"吃饭").start();
        Thread.sleep(1000l);
        new OutEventThread(queue,"开发").start();
        new OutEventThread(queue,"收快递").start();
        Thread.sleep(2000l);
        new OutEventThread(queue,"吃午饭").start();
        Thread.sleep(1500l);
        new OutEventThread(queue,"开会").start();;
        new OutEventThread(queue,"继续编码").start();
        new OutEventThread(queue,"下午茶").start();
        Thread.sleep(1000l);
        new OutEventThread(queue,"继续开会").start();
        new OutEventThread(queue,"晚饭").start();
        Thread.sleep(1000l);
        new OutEventThread(queue,"睡觉").start();
        Thread.sleep(1000l);

        body.close();

    }
}

 

Java设计模式是一组经过实践验证的面向对象设计原则和模式,可以帮助开发人员解决常见的软件设计问题。下面是常见的23种设计模式: 1. 创建型模式(Creational Patterns): - 工厂方法模式(Factory Method Pattern) - 抽象工厂模式(Abstract Factory Pattern) - 单例模式(Singleton Pattern) - 原型模式(Prototype Pattern) - 建造者模式(Builder Pattern) 2. 结构型模式(Structural Patterns): - 适配器模式(Adapter Pattern) - 桥接模式(Bridge Pattern) - 组合模式(Composite Pattern) - 装饰器模式(Decorator Pattern) - 外观模式(Facade Pattern) - 享元模式(Flyweight Pattern) - 代理模式(Proxy Pattern) 3. 行为型模式(Behavioral Patterns): - 责任链模式(Chain of Responsibility Pattern) - 命令模式(Command Pattern) - 解释器模式(Interpreter Pattern) - 迭代器模式(Iterator Pattern) - 中介者模式(Mediator Pattern) - 备忘录模式(Memento Pattern) - 观察者模式(Observer Pattern) - 状态模式(State Pattern) - 策略模式(Strategy Pattern) - 模板方法模式(Template Method Pattern) - 访问者模式(Visitor Pattern) 4. 并发型模式(Concurrency Patterns): - 保护性暂停模式Guarded Suspension Pattern) - 生产者-消费者模式(Producer-Consumer Pattern) - 读写锁模式(Read-Write Lock Pattern) - 信号量模式(Semaphore Pattern) - 线程池模式(Thread Pool Pattern) 这些设计模式可以根据问题的特点和需求来选择使用,它们提供了一些可复用的解决方案,有助于开发高质量、可维护且易于扩展的软件系统。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值