Guarded Suspension

我们来看一个例子:

A 在厨房做早餐

B是快递员,敲门了,说你的快递来了,要求开门,

那么A就需要去取快递,一会又来一个快递,那么A就需要做饭的同时还要取快递。如何去设计这个程序呢?
我们来看代码实例:

package com.handsome.thread2study.chapter9;

/**
 * @author jiangkunli
 * @date 2020-07-14 12:32 上午
 * @description 定义请求类
 */
public class Request {

    private final String value;

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

    public String getValue() {
        return value;
    }
}




package com.handsome.thread2study.chapter9;

import java.sql.ResultSet;
import java.util.LinkedList;

/**
 * @author jiangkunli
 * @date 2020-07-14 12:32 上午
 * @description 定义请求队列 用来存放数据
 */
public class RequestQueue {

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

    public Request request;

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

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

}



package com.handsome.thread2study.chapter9;

import java.util.Random;

/**
 * @author jiangkunli
 * @date 2020-07-14 12:44 上午
 * @description 服务线程 A 时刻监听变化 并去取快递
 */
public class ServerThread extends Thread{

    private final RequestQueue requestQueue;

    private volatile boolean flag = true;

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

    @Override
    public void run() {
        while (flag){
            Request request = requestQueue.getRequest();
            if(null == request){
                continue;
            }
            System.out.println(request.getValue());
            try {
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

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


package com.handsome.thread2study.chapter9;

import java.util.Random;

/**
 * @author jiangkunli
 * @date 2020-07-14 12:41 上午
 * @description 定义快递人员
 */ 
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;
        this.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();
            }
        }
    }
}


package com.handsome.thread2study.chapter9;

/**
 * @author jiangkunli
 * @date 2020-07-14 12:53 上午
 * @description 启动线程 
 */
public class SuspensionClient {

    public static void main(String[] args) {
        final RequestQueue queue = new RequestQueue();
        new ClientThread(queue,"Alex").start();
        ServerThread serverThread = new ServerThread(queue);

        serverThread.start();
        try {
            serverThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        serverThread.close();
    }

}


我们看到 client 作为快递员不断的将快递放入队列,监听人员则不断的取出快递。

这就是我们所说的保护性暂存模式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值