关闭

多线程设计模式之Guarded Suspension模式

标签: 设计模式多线程并发
209人阅读 评论(0) 收藏 举报
分类:

Guarded Suspension意为保护暂停,就是说把客户端的请求排队,由服务器程序一个一个处理。
当客户端的请求数量超过了服务线程的处理能力,在频繁的客户端请求中,RequestQueue充当了中间缓存,存放未处理的请求,保证了客户请求不丢失,同时也保护了服务线程不会受到大量并发的请求,而导致计算机资源不足。

给出一个简单实现。

1、请求类Request

package javacurrent.guardedsuspension;

public class Request {
    private String name;
    public Request(String name){
        this.name = name;
    }

    public String getName(){
        return name;
    }

    public String toString(){
        return "[Request" + name + "]";
    }
}

2、Request的集合类RequestQueue

package javacurrent.guardedsuspension;

import java.util.LinkedList;

public class RequestQueue {
    private java.util.LinkedList queue = new LinkedList();
    public synchronized Request getRequest(){
        while(queue.size() == 0){
            try {
                wait();
            } catch (InterruptedException e) {
            }
        }
        return (Request)queue.remove();
    }
    public synchronized void addRequest(Request request){
        queue.add(request);
        notifyAll();
    }
}

3、服务端进程,用于处理客户端请求。

package javacurrent.guardedsuspension;

public class ServerThread extends Thread {
    private RequestQueue queue;

    public ServerThread(RequestQueue queue, String name){
        super(name);
        this.queue = queue;
    }

    public void run() {
        while (true) {
            final Request request = queue.getRequest();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
            System.out.println(Thread.currentThread().getName() + " handles " + request);
        }
    }
}

4、客户端进程,用于给服务端发起请求

package javacurrent.guardedsuspension;

public class ClientThread extends Thread {
    private RequestQueue queue;

    public ClientThread(RequestQueue queue, String name) {
        super(name);
        this.queue = queue;
    }

    public void run() {
        for (int i = 0; i < 10; i++) {
            Request request = new Request("RequestID:" + i + " Thread_Name:"
                    + Thread.currentThread().getName());
            queue.addRequest(request);
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
            }
            System.out.println("ClientThread Name is:" + Thread.currentThread().getName());
        }
        System.out.println(Thread.currentThread().getName() + " request end");
    }
}

5、Main类,开启了10个Client进程和Server进程,Client进程的请求速度快于Server进程的处理请求速度,于是RequestQueue发挥了中间缓存的作用。

package javacurrent.guardedsuspension;

public class Main {
    public static void main(String[] args) {
        RequestQueue queue = new RequestQueue();
        for (int i = 0; i < 10; i++) {
            new ClientThread(queue, "ClientThread " + i).start();
        }
        for (int i = 0; i < 10; i++) {
            new ServerThread(queue, "ServerThread " + i).start();
        }
    }
}

6、测试结果
ClientThread Name is:ClientThread 6
ClientThread Name is:ClientThread 2
ClientThread Name is:ClientThread 0
ClientThread Name is:ClientThread 1
ClientThread Name is:ClientThread 4
ClientThread Name is:ClientThread 2
ClientThread Name is:ClientThread 6
ClientThread Name is:ClientThread 7
ClientThread Name is:ClientThread 3
ServerThread 1 handles [RequestRequestID:0 Thread_Name:ClientThread 0]
ServerThread 5 handles [RequestRequestID:0 Thread_Name:ClientThread 6]
ServerThread 9 handles [RequestRequestID:0 Thread_Name:ClientThread 9]
ServerThread 8 handles [RequestRequestID:0 Thread_Name:ClientThread 8]
ServerThread 0 handles [RequestRequestID:0 Thread_Name:ClientThread 2]
ServerThread 4 handles [RequestRequestID:0 Thread_Name:ClientThread 5]
ClientThread Name is:ClientThread 8
ClientThread Name is:ClientThread 9
ClientThread Name is:ClientThread 5
ServerThread 6 handles [RequestRequestID:0 Thread_Name:ClientThread 3]
ServerThread 2 handles [RequestRequestID:0 Thread_Name:ClientThread 4]
ServerThread 3 handles [RequestRequestID:0 Thread_Name:ClientThread 1]
ServerThread 7 handles [RequestRequestID:0 Thread_Name:ClientThread 7]
ClientThread Name is:ClientThread 4
ClientThread Name is:ClientThread 8
。。。。。。。
。。。。。。
。。。。。。
ClientThread 5 request end
ClientThread Name is:ClientThread 1
ClientThread 1 request end
ClientThread Name is:ClientThread 0
ClientThread 0 request end
ClientThread Name is:ClientThread 4
ClientThread 4 request end
ClientThread Name is:ClientThread 3
ClientThread 3 request end
ClientThread Name is:ClientThread 2
ClientThread 2 request end
ClientThread 6 request end
ServerThread 1 handles [RequestRequestID:1 Thread_Name:ClientThread 3]
ServerThread 5 handles [RequestRequestID:1 Thread_Name:ClientThread 8]
ServerThread 9 handles [RequestRequestID:1 Thread_Name:ClientThread 7]
ServerThread 8 handles [RequestRequestID:1 Thread_Name:ClientThread 9]
ServerThread 0 handles [RequestRequestID:1 Thread_Name:ClientThread 6]
ServerThread 4 handles [RequestRequestID:1 Thread_Name:ClientThread 4]
ServerThread 6 handles [RequestRequestID:1 Thread_Name:ClientThread 5]
ServerThread 2 handles [RequestRequestID:1 Thread_Name:ClientThread 2]

可见最后客户端都退出了,由于请求未处理完,服务端继续处理请求,直到请求全部处理为止。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:7194次
    • 积分:229
    • 等级:
    • 排名:千里之外
    • 原创:16篇
    • 转载:0篇
    • 译文:0篇
    • 评论:6条
    最新评论