java多线程:Guarded Suspension设计模式

Guarded Suspension设计模式:Guarded是被保护的,Suspension 暂停的,当一个线程正在做某件事,这件事的优先级比较高不想被打断,可以考虑该设计模式。


/**
 * Guarded Suspension设计模式
 * 请求
 *
 */
@Getter
public class Request {
    private final String value;

    public Request(String value) {
        this.value = value;
    }
}
/**
 * Guarded Suspension设计模式
 * 请求队列
 *
 */
public class RequestQueue {
    /**
     * 存放请求的队列
     */
    private final LinkedList<Request> queue = new LinkedList<>();

    /**
     * 从队列获取请求Request
     *
     * @return 请求Request
     */
    public Request getRequest() {
        synchronized (queue) {
            while (queue.size() <= 0) {
                try {
                    queue.wait();
                } catch (InterruptedException e) {
                    return null;
                }
            }

            return queue.removeFirst();
        }
    }

    /**
     * 向队列放入请求Request
     *
     * @param request 请求Request
     */
    public void putRequest(Request request) {
        synchronized (queue) {
            queue.addLast(request);
            queue.notifyAll();
        }
    }
}
//===========================================
/**
 * Guarded Suspension设计模式
 * 发起请求的客户端线程
 *
 */
public class ClientThread extends Thread {
    /**
     * 存储请求的队列
     */
    private final RequestQueue queue;

    /**
     * 发送值
     */
    private final String sendValue;

    /**
     * 随机数变量
     */
    private final Random random;

    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();
            }
        }
    }
}
/**
 * Guarded Suspension设计模式
 * 接收请求的客户端线程
 *
 */
public class ServerThread extends Thread {
    /**
     * 存储请求的队列
     */
    private final RequestQueue queue;

    /**
     * 随机数变量
     */
    private final Random random;

    /**
     * 控制线程是否结束
     */
    private volatile boolean flag = true;

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

    @Override
    public void run() {
        // 三个地方可以中断:
        // 1. 执行完成时,修改flag
        while (flag) {
            // 2. getRequest陷入wait状态
            Request request = queue.getRequest();
            if (null == request) {
                System.out.println("Received the empty request.");
                continue;
            }
            System.out.println("Server -> " + request.getValue());
            try {
                // 3. sleep状态
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {
                return;
            }
        }
    }

    /**
     * 修改标志变量,结束线程
     */
    public void close() {
        this.flag = false;
        this.interrupt();
    }
}
//========================================
/**
 * Guarded Suspension设计模式
 * 测试
 *
 */
public class SuspensionClient {
    public static void main(String[] args) throws InterruptedException {
        RequestQueue queue = new RequestQueue();
        new ClientThread(queue, "Alex").start();
        ServerThread serverThread = new ServerThread(queue);
        serverThread.start();
        Thread.sleep(10_000L);
        serverThread.close();
    }
}

你可能感兴趣的:(设计模式,java,设计模式,多线程)