1、任何的高并发,请求总是会有一个顺序的
2、java的队列的数据结构是先进先出的取值顺序
3、BlockingQueue类(线程安全)(使用方法可以百度)
一般使用LinkedBlockingQueue
利用以上几点,我们可以把高并发时候的请求放入一个队列,队列的大小可以自己定义,比如队列容量为1000个数据,那么可以利用过滤器或者拦截器把当前的请求放入队列,如果队列的容量满了,其余的请求可以丢掉或者作出相应回复
具体实施:
利用生产者、消费者模型:
将队列的请求一一处理完。
上代码:
/**
* @author fuguangli
* @description 前沿消费者类
* @Create date: 2017/3/7
* @using EXAMPLE
*/
public class Customer implements Runnable{
/**
* 抛出异常 特殊值 阻塞 超时
插入 add(e) offer(e) put(e) offer(e, time, unit)
移除 remove() poll() take() poll(time, unit)
检查 element() peek() 不可用 不可用
*/
private BlockingQueue blockingQueue;
private AtomicInteger count = new AtomicInteger();
public Customer(BlockingQueue blockingQueue) {
this.blockingQueue = blockingQueue;
}
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p/>
* The general c