实施这次看门狗有什么缺陷吗?

时间:2018-10-28 19:00:05

标签: java executorservice

此代码在使用线程方面是否有重大缺陷?我的目的是添加一个时间监视器来控制允许返回响应的最长时间。最长时间设置为WATCH_DOG_TIME_LIMIT。如果处理时间超过该时间,它将超时并返回默认响应。

public SimilarityResponse getSimilarity(InputInfo questionInput) {

        Preconditions.checkNotNull(questionInput);

        Callable<SimilarityResponse> queryTask = () -> similarityService.computeSimilarity(questionInput);
        Future<SimilarityResponse> futureTask = executorService.submit(queryTask);
        try {
            SimilarityResponse response = futureTask.get(WATCH_DOG_TIME_LIMIT, TimeUnit.MILLISECONDS);
            LOGGER.info("AfterExtraction(): {}", questionInput);
            return response;
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        } catch (ExecutionException ee) {
            ee.printStackTrace();
        } catch (TimeoutException te) {
            futureTask.cancel(true);
            LOGGER.warn("QE L0 tiring: {} {}", WATCH_DOG_TIME_LIMIT, questionInput);
        }

        // Only when timeout occurs, execute this.
        SimilarityResponse sr = new SimilarityResponse();
        sr.setSku(questionInput.getSku());
        sr.setText(questionInput.getQuestion());
        return sr;

    }

第二个问题,如果没有正确性问题,是否存在效率问题?必须再次调用此函数。每个调用都会创建一个新的Callable实例。这样好吗

0 个答案:

没有答案