多线程设计模式-高并发请求缓存模式(Guarded Suspension)

高并发请求缓存队列设计模式

一,什么是Guarded Suspension模式
如果执行现在的处理会造成问题,就让执行处理的线程等待。这种模式通过让线程等待来保证实例的安全性

二,实现一个简单的线程间通信的例子

一个线程(ClientThread)将请求(Request)的实例传递给另外一个线程(ServerThread)

Request:线程实例

RequestQueue:存放请求(Request)实例的队列

ClientThread:把线程实例放到队列中

ServerThread:从队列中取线程示例

示例程序

package com.ln.concurrent.chapter9;

/**
 * @ProjectName: java-concurrency
 * @Package: com.ln.concurrent.chapter9
 * @Name:Request
 * @Author:linianest
 * @CreateTime:2020/3/28 19:29
 * @version:1.0
 * @Description TODO: Request请求
 */
public class Request {
    private final String value;

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

    public String getValue() {
        return value;
    }
}

请求队列

package com.ln.concurrent.chapter9;

import java.util.LinkedList;

/**
 * @ProjectName: java-concurrency
 * @Package: com.ln.concurrent.chapter9
 * @Name:RequestQueue
 * @Author:linianest
 * @CreateTime:2020/3/28 19:31
 * @version:1.0
 * @Description TODO: 请求队列
 */
public class RequestQueue {
    private final LinkedList<Request> queue = new LinkedList<>();

    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.ln.concurrent.chapter9;

import java.util.Random;

/**
 * @ProjectName: java-concurrency
 * @Package: com.ln.concurrent.chapter9
 * @Name:ClientThread
 * @Author:linianest
 * @CreateTime:2020/3/28 19:55
 * @version:1.0
 * @Description TODO: 客户端线程
 */
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(1_000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

服务端处理线程

package com.ln.concurrent.chapter9;

import java.util.Random;

/**
 * @ProjectName: java-concurrency
 * @Package: com.ln.concurrent.chapter9
 * @Name:ServerThread
 * @Author:linianest
 * @CreateTime:2020/3/28 20:03
 * @version:1.0
 * @Description TODO: 服务端处理线程
 */
public class ServerThread extends Thread {

    private final RequestQueue queue;

    private final Random random;

    private volatile boolean closed = false;

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


    @Override
    public void run() {
        while (!closed) {
            Request request = queue.getRequest();
            if (null == request) {
                System.out.println("Received the empty request.");
                continue;
            }
            System.out.println("Server -> " + request.getValue());
            try {
                Thread.sleep(random.nextInt(1_000));
            } catch (InterruptedException e) {
                return;
            }
        }
    }

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

高并发请求,缓存队列

package com.ln.concurrent.chapter9;

/**
 * @ProjectName: java-concurrency
 * @Package: com.ln.concurrent.chapter9
 * @Name:SuspensionClient
 * @Author:linianest
 * @CreateTime:2020/3/28 20:20
 * @version:1.0
 * @Description TODO: 高并发请求,缓存队列
 */
public class SuspensionClient {
    public static void main(String[] args) throws InterruptedException {
        final RequestQueue queue = new RequestQueue();
        new ClientThread(queue, "Alex").start();
        ServerThread serverThread = new ServerThread(queue);
        serverThread.start();
//        serverThread.join();

        Thread.sleep(10000);

        serverThread.close();
    }
}

这种实现了,将高并发请求缓存在队列中,当前面的任务线程执行完成后,按照顺序执行缓存的任务请求,保护服务器

上一篇:centos7 上 创建软连接 ln -s


下一篇:P4389 付公主的背包