一个request请求然后锁定等待异步接口处理结果
private ConcurrentHashMap<String, Locker> relation = new ConcurrentHashMap<String, Locker>(); protected void doGet(HttpServletRequest request, final HttpServletResponse response) { final String id = request.getParameter("id"); if(null != id){ final Locker locker = relation.get(id); //此处的效率? if(locker == null){ Locker lock = new Locker(id, false); synchronized(lock){ try{ relation.put(id, lock); System.out.println(id + "开始等待,relation.size() = " + relation.size()); lock.wait(5000l); //设置超时时间 HttpClientUtil.httpGetRequest("http://localhost:8080/Test/BusiServlet?id=" + 2); System.out.println(id + "同步处理完成,relation.size() = " + relation.size()); }catch(InterruptedException e){ e.printStackTrace(); } } try{ response.serHeader("content-type", "text/html;charset=utf-8"); if(relation.containsKey(id)){ response.getOutputStream().write(("失败...." + id).getBytes("utf-8")); } else { response.getOutputStream().write(("成功...." + id).getBytes("utf-8")); } }catch(IOException e){ e.printStackTrace(); } } else { //此处需要加锁? synchronized(locker){ try{ locker.notify(); }catch(Exception e){ e.printStackTrace(); } relation.remove(id); } } } } class Locker{ String id; boolean dir; //Getter and Setter }