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

    }
}

 

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