Guarded Suspension模式

    服务器可能在很短时间内承受大量的客户端请求,客户端请求的数量可能超过服务器本身即时处理能力,而服务端程序又不能丢弃任何一个客户请求。此时最佳处理方案莫过于让客户端请求进行排队,由服务端程序一个接一个处理,这样既保证了所有客户端请求均不丢失,同时也避免了服务器由于同时处理太多请求而崩溃。

public class Request {
    private String name;
	public Request(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public String toString() {
        return "[ Request " + name + " ]";
    }
}
import java.util.LinkedList;

public class RequestQueue {
    private 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();
    }
}
public class ServerThread extends Thread {
    private RequestQueue requestQueue;
    public ServerThread(RequestQueue requestQueue, String name) {
        super(name);
        this.requestQueue = requestQueue;
    }
    public void run() {
        while (true) {
            final Request request = requestQueue.getRequest();
            try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
            System.out.println(Thread.currentThread().getName() + " handles  " + request);
            
        }
    }
}
public class ClientThread extends Thread {
    private RequestQueue requestQueue;
    public ClientThread(RequestQueue requestQueue, String name) {
        super(name);
        this.requestQueue = requestQueue;
    }
    public void run() {
        for (int i = 0; i < 10; i++) {
            Request request = new Request("RequestID:" + i+" Thread_Name:"+Thread.currentThread().getName());
            System.out.println(Thread.currentThread().getName() + " requests " + request);
            requestQueue.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");
    }
}
public class Main {
    public static void main(String[] args) {
        RequestQueue requestQueue = new RequestQueue();
        for(int i=0;i<5;i++) //创建5个服务端线程
        	new ServerThread(requestQueue, "ServerThread"+i).start();
        for(int i=0;i<5;i++) //创建5个客户端线程
        	new ClientThread(requestQueue, "ClientThread"+i).start();
    }
}

输出结果

ClientThread0 requests [ Request RequestID:0 Thread_Name:ClientThread0 ]
ClientThread1 requests [ Request RequestID:0 Thread_Name:ClientThread1 ]
ClientThread2 requests [ Request RequestID:0 Thread_Name:ClientThread2 ]
ClientThread3 requests [ Request RequestID:0 Thread_Name:ClientThread3 ]
ClientThread4 requests [ Request RequestID:0 Thread_Name:ClientThread4 ]
ClientThread Name is:ClientThread0
ClientThread Name is:ClientThread1
ClientThread0 requests [ Request RequestID:1 Thread_Name:ClientThread0 ]
ClientThread1 requests [ Request RequestID:1 Thread_Name:ClientThread1 ]
ClientThread Name is:ClientThread2
ClientThread Name is:ClientThread3
ClientThread Name is:ClientThread4
ClientThread4 requests [ Request RequestID:1 Thread_Name:ClientThread4 ]
ClientThread3 requests [ Request RequestID:1 Thread_Name:ClientThread3 ]
ClientThread2 requests [ Request RequestID:1 Thread_Name:ClientThread2 ]
ClientThread Name is:ClientThread1
ClientThread Name is:ClientThread0
ClientThread0 requests [ Request RequestID:2 Thread_Name:ClientThread0 ]
ClientThread1 requests [ Request RequestID:2 Thread_Name:ClientThread1 ]
ClientThread Name is:ClientThread2
ClientThread Name is:ClientThread3
ClientThread Name is:ClientThread4
ClientThread2 requests [ Request RequestID:2 Thread_Name:ClientThread2 ]
ClientThread4 requests [ Request RequestID:2 Thread_Name:ClientThread4 ]
ClientThread3 requests [ Request RequestID:2 Thread_Name:ClientThread3 ]
ClientThread Name is:ClientThread1
ClientThread Name is:ClientThread0
ClientThread1 requests [ Request RequestID:3 Thread_Name:ClientThread1 ]
ClientThread0 requests [ Request RequestID:3 Thread_Name:ClientThread0 ]
ClientThread Name is:ClientThread2
ClientThread Name is:ClientThread3
ClientThread Name is:ClientThread4
ClientThread3 requests [ Request RequestID:3 Thread_Name:ClientThread3 ]
ClientThread2 requests [ Request RequestID:3 Thread_Name:ClientThread2 ]
ClientThread4 requests [ Request RequestID:3 Thread_Name:ClientThread4 ]
ClientThread Name is:ClientThread0
ClientThread0 requests [ Request RequestID:4 Thread_Name:ClientThread0 ]
ClientThread Name is:ClientThread4
ClientThread4 requests [ Request RequestID:4 Thread_Name:ClientThread4 ]
ClientThread Name is:ClientThread2
ClientThread Name is:ClientThread1
ClientThread1 requests [ Request RequestID:4 Thread_Name:ClientThread1 ]
ClientThread2 requests [ Request RequestID:4 Thread_Name:ClientThread2 ]
ClientThread Name is:ClientThread3
ClientThread3 requests [ Request RequestID:4 Thread_Name:ClientThread3 ]
ClientThread Name is:ClientThread3
ClientThread3 requests [ Request RequestID:5 Thread_Name:ClientThread3 ]
ClientThread Name is:ClientThread2
ClientThread Name is:ClientThread0
ClientThread Name is:ClientThread4
ClientThread Name is:ClientThread1
ClientThread4 requests [ Request RequestID:5 Thread_Name:ClientThread4 ]
ClientThread1 requests [ Request RequestID:5 Thread_Name:ClientThread1 ]
ClientThread0 requests [ Request RequestID:5 Thread_Name:ClientThread0 ]
ClientThread2 requests [ Request RequestID:5 Thread_Name:ClientThread2 ]
ClientThread Name is:ClientThread2
ClientThread Name is:ClientThread3
ClientThread Name is:ClientThread4
ClientThread Name is:ClientThread1
ClientThread Name is:ClientThread0
ClientThread1 requests [ Request RequestID:6 Thread_Name:ClientThread1 ]
ClientThread4 requests [ Request RequestID:6 Thread_Name:ClientThread4 ]
ClientThread3 requests [ Request RequestID:6 Thread_Name:ClientThread3 ]
ClientThread2 requests [ Request RequestID:6 Thread_Name:ClientThread2 ]
ClientThread0 requests [ Request RequestID:6 Thread_Name:ClientThread0 ]
ClientThread Name is:ClientThread0
ClientThread Name is:ClientThread1
ClientThread Name is:ClientThread4
ClientThread Name is:ClientThread2
ClientThread Name is:ClientThread3
ClientThread2 requests [ Request RequestID:7 Thread_Name:ClientThread2 ]
ClientThread4 requests [ Request RequestID:7 Thread_Name:ClientThread4 ]
ClientThread1 requests [ Request RequestID:7 Thread_Name:ClientThread1 ]
ClientThread0 requests [ Request RequestID:7 Thread_Name:ClientThread0 ]
ClientThread3 requests [ Request RequestID:7 Thread_Name:ClientThread3 ]
ClientThread Name is:ClientThread3
ClientThread3 requests [ Request RequestID:8 Thread_Name:ClientThread3 ]
ClientThread Name is:ClientThread0
ClientThread0 requests [ Request RequestID:8 Thread_Name:ClientThread0 ]
ClientThread Name is:ClientThread1
ClientThread1 requests [ Request RequestID:8 Thread_Name:ClientThread1 ]
ClientThread Name is:ClientThread4
ClientThread4 requests [ Request RequestID:8 Thread_Name:ClientThread4 ]
ClientThread Name is:ClientThread2
ClientThread2 requests [ Request RequestID:8 Thread_Name:ClientThread2 ]
ServerThread4 handles  [ Request RequestID:0 Thread_Name:ClientThread0 ]
ServerThread0 handles  [ Request RequestID:0 Thread_Name:ClientThread1 ]
ClientThread Name is:ClientThread4
ClientThread Name is:ClientThread3
ClientThread Name is:ClientThread0
ServerThread1 handles  [ Request RequestID:0 Thread_Name:ClientThread3 ]
ClientThread Name is:ClientThread1
ServerThread3 handles  [ Request RequestID:0 Thread_Name:ClientThread2 ]
ClientThread0 requests [ Request RequestID:9 Thread_Name:ClientThread0 ]
ClientThread3 requests [ Request RequestID:9 Thread_Name:ClientThread3 ]
ServerThread2 handles  [ Request RequestID:0 Thread_Name:ClientThread4 ]
ClientThread4 requests [ Request RequestID:9 Thread_Name:ClientThread4 ]
ClientThread1 requests [ Request RequestID:9 Thread_Name:ClientThread1 ]
ClientThread Name is:ClientThread2
ClientThread2 requests [ Request RequestID:9 Thread_Name:ClientThread2 ]
ClientThread Name is:ClientThread2
ClientThread2 request end
ClientThread Name is:ClientThread4
ClientThread4 request end
ClientThread Name is:ClientThread3
ClientThread3 request end
ClientThread Name is:ClientThread0
ClientThread0 request end
ClientThread Name is:ClientThread1
ClientThread1 request end
ServerThread0 handles  [ Request RequestID:1 Thread_Name:ClientThread1 ]
ServerThread4 handles  [ Request RequestID:1 Thread_Name:ClientThread0 ]
ServerThread2 handles  [ Request RequestID:1 Thread_Name:ClientThread2 ]
ServerThread1 handles  [ Request RequestID:1 Thread_Name:ClientThread4 ]
ServerThread3 handles  [ Request RequestID:1 Thread_Name:ClientThread3 ]
ServerThread4 handles  [ Request RequestID:2 Thread_Name:ClientThread1 ]
ServerThread0 handles  [ Request RequestID:2 Thread_Name:ClientThread0 ]
ServerThread2 handles  [ Request RequestID:2 Thread_Name:ClientThread2 ]
ServerThread3 handles  [ Request RequestID:2 Thread_Name:ClientThread3 ]
ServerThread1 handles  [ Request RequestID:2 Thread_Name:ClientThread4 ]
ServerThread4 handles  [ Request RequestID:3 Thread_Name:ClientThread1 ]
ServerThread0 handles  [ Request RequestID:3 Thread_Name:ClientThread0 ]
ServerThread3 handles  [ Request RequestID:3 Thread_Name:ClientThread2 ]
ServerThread2 handles  [ Request RequestID:3 Thread_Name:ClientThread3 ]
ServerThread1 handles  [ Request RequestID:3 Thread_Name:ClientThread4 ]
ServerThread0 handles  [ Request RequestID:4 Thread_Name:ClientThread4 ]
ServerThread4 handles  [ Request RequestID:4 Thread_Name:ClientThread0 ]
ServerThread1 handles  [ Request RequestID:4 Thread_Name:ClientThread3 ]
ServerThread3 handles  [ Request RequestID:4 Thread_Name:ClientThread2 ]
ServerThread2 handles  [ Request RequestID:4 Thread_Name:ClientThread1 ]
ServerThread4 handles  [ Request RequestID:5 Thread_Name:ClientThread4 ]
ServerThread0 handles  [ Request RequestID:5 Thread_Name:ClientThread3 ]
ServerThread2 handles  [ Request RequestID:5 Thread_Name:ClientThread2 ]
ServerThread1 handles  [ Request RequestID:5 Thread_Name:ClientThread1 ]
ServerThread3 handles  [ Request RequestID:5 Thread_Name:ClientThread0 ]
ServerThread3 handles  [ Request RequestID:6 Thread_Name:ClientThread0 ]
ServerThread1 handles  [ Request RequestID:6 Thread_Name:ClientThread2 ]
ServerThread2 handles  [ Request RequestID:6 Thread_Name:ClientThread3 ]
ServerThread0 handles  [ Request RequestID:6 Thread_Name:ClientThread4 ]
ServerThread4 handles  [ Request RequestID:6 Thread_Name:ClientThread1 ]
ServerThread3 handles  [ Request RequestID:7 Thread_Name:ClientThread2 ]
ServerThread1 handles  [ Request RequestID:7 Thread_Name:ClientThread4 ]
ServerThread2 handles  [ Request RequestID:7 Thread_Name:ClientThread1 ]
ServerThread0 handles  [ Request RequestID:7 Thread_Name:ClientThread0 ]
ServerThread4 handles  [ Request RequestID:7 Thread_Name:ClientThread3 ]
ServerThread4 handles  [ Request RequestID:8 Thread_Name:ClientThread2 ]
ServerThread0 handles  [ Request RequestID:8 Thread_Name:ClientThread4 ]
ServerThread2 handles  [ Request RequestID:8 Thread_Name:ClientThread1 ]
ServerThread1 handles  [ Request RequestID:8 Thread_Name:ClientThread0 ]
ServerThread3 handles  [ Request RequestID:8 Thread_Name:ClientThread3 ]
ServerThread3 handles  [ Request RequestID:9 Thread_Name:ClientThread2 ]
ServerThread1 handles  [ Request RequestID:9 Thread_Name:ClientThread1 ]
ServerThread2 handles  [ Request RequestID:9 Thread_Name:ClientThread4 ]
ServerThread0 handles  [ Request RequestID:9 Thread_Name:ClientThread3 ]
ServerThread4 handles  [ Request RequestID:9 Thread_Name:ClientThread0 ]


参考:Java程序性能优化


猜你喜欢

转载自blog.csdn.net/andy2019/article/details/79815807
今日推荐