class ThreadProtectedFilter implements InitializableFilter {
    protected static ConcurrentHashMap<String, AtomicInteger> portTotalMap = new ConcurrentHashMap<String, AtomicInteger>();
    protected ConcurrentHashMap<String, AtomicInteger> methodMap = new ConcurrentHashMap<String, AtomicInteger>();
    protected AtomicInteger totalCount;
    protected int maxThread;
    protected int totalLimit;
    protected int methodLimit;
    protected boolean isProvider = false;

    @Override
    public Response filter(Caller<?> caller, Request request) {
        if (isProvider) {
            String requestKey = MotanFrameworkUtil.getFullMethodString(request);
            AtomicInteger methodCount = methodMap.get(requestKey);
            if (methodCount == null) {
                methodMap.putIfAbsent(requestKey, new AtomicInteger());
                methodCount = methodMap.get(requestKey);
            }
            try {
                int tCount = totalCount.incrementAndGet();
                int mCount = methodCount.incrementAndGet();
                if (tCount > totalLimit && mCount > methodLimit) {
                    return reject(request.getInterfaceName() + "." + request.getMethodName(), mCount, tCount);
                }
                return caller.call(request);
            } finally {
                totalCount.decrementAndGet();
                methodCount.decrementAndGet();
            }
        } else {
            return caller.call(request);
        }

    }

    private Response reject(String method, int requestCounter, int totalCounter) {<FILL_FUNCTION_BODY>}


    /**
     * 默认策略当接口线程池占用达到3/4或者空闲小于150时，限制单个方法请求不能超过总线程数的1/2 需要自定义方法并发限制可以通过actives参数配置
     */
    @Override
    public void init(Caller<?> caller) {
        if (caller instanceof Provider) {
            String port = String.valueOf(caller.getUrl().getPort());
            totalCount = portTotalMap.get(port);
            if (totalCount == null) {
                portTotalMap.putIfAbsent(port, new AtomicInteger());
                totalCount = portTotalMap.get(port);
            }
            maxThread = caller.getUrl().getIntParameter(URLParamType.maxWorkerThread.getName(), URLParamType.maxWorkerThread.getIntValue());
            totalLimit = maxThread > 600 ? maxThread - 150 : maxThread * 3 / 4;
            int active = caller.getUrl().getIntParameter(URLParamType.actives.getName(), URLParamType.actives.getIntValue());
            if (active > 0) {
                methodLimit = active;
            } else {
                methodLimit = maxThread / 2;
            }
            isProvider = true;
        }
    }

}

        DefaultResponse response = new DefaultResponse();
        MotanServiceException exception =
                new MotanServiceException("ThreadProtectedFilter reject request: request_counter=" + requestCounter + " total_counter="
                        + totalCounter + " max_thread=" + maxThread, MotanErrorMsgConstant.SERVICE_REJECT, false);
        response.setException(exception);
        LoggerUtil.error("ThreadProtectedFilter reject request: request_method=" + method + " request_counter=" + requestCounter + " ="
                + totalCounter + " max_thread=" + maxThread);
        return response;
    
<no_super_class>