并发编程 - Guarded Suspension模式

Guarded Suspension意为保护暂停,其核心思想是仅当服务进程准备好时,才提供服务。设想一种场景,服务器可能在很短时间内承受大量的客户请求,客户端请求的数量可能超过服务器本身的处理能力,而服务器又不能丢弃任何一个客户请求。此时,最佳的处理方案莫过于让客户端请求排队,由服务端程序一个接一个处理,这样,既保证了所有的客户端程序不丢失,同时也避免了服务器由于处理太多的请求而崩溃。

1.Guarded Suspension模式的结构

Guarded Suspension模式模式的主要成员如下:

角色 作用
Request 表示客户端请求
RequestQueue 用于保存客户端请求队列
ClientThread 客户端进程
ServerThread 服务器进程

                                                    表 1-1

ServerThread则根据其自身的状态,在有能力处理请求时,从RequestQueue中提取请求对象加以处理。系统的工作流程如图

并发编程 - Guarded Suspension模式_第1张图片
                                                        图1-2

2.Guarded Suspension模式的简单实现

2.1Request类

public class Request {

	private String name;

	public Request(String name){
		this.name = name;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "[ Request  " + name + "  ]";
	}



2.2RequestQueue


public class RequestQueue {

	private LinkedList<Request> queue = new LinkedList<Request>();
	
	public synchronized Request getRequest(){
		while(queue.size() == 0){
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return  queue.remove();
	}
	
	public synchronized void addRequest(Request req){
		
		queue.add(req);    // 加入新的请求
		notifyAll();		// 通知getRequest()方法
		
	}
	
	
}



2.3ServerThread

public class ServerThread extends Thread {

	private RequestQueue requestQueue = null;
	
	public ServerThread(RequestQueue requestQueue, String name){
		super(name);
		this.requestQueue = requestQueue;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		
		while(true){
			final Request request = requestQueue.getRequest();
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			System.out.println(Thread.currentThread().getName() + "handles " + request);
		}
		
	}
	
}




2.4ClientThread

public class ClientThread extends Thread {

	private RequestQueue requestQueue = null;
	
	public ClientThread(RequestQueue requestQueue, String name){
		super(name);
		this.requestQueue = requestQueue;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		
		for(int i = 0; i < 10; i++){
			Request request = new Request("RequestID:" + i + " Thread_Name:" + Thread.currentThread().getName());
			
			System.out.println("RequestID:" + i + " Thread_Name:" + Thread.currentThread().getName());
			
			requestQueue.addRequest(request);
			
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			System.out.println("ClientThread Name is :" + Thread.currentThread().getName());
		}
		
		System.out.println(Thread.currentThread().getName() + " request end");
		
		
	}




main

public class testmain {

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




你可能感兴趣的:(并发编程 - Guarded Suspension模式)