一天的事情---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();

    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章