如何在Concurrent Future Java中防止多线程响应的交换?

时间:2019-05-08 16:31:27

标签: java multithreading future concurrent.futures futuretask

检查以下代码。多线程中存在一个问题,其中每个线程的结果都被交换

我们尝试过使用withWheelColor.isDone和withWheelColor.isCancelled,并且也用withWheelColor.get()包围了它,但是没有运气

    final Map<String, Object> result = new HashMap<>();
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    ProductSearchPageData<SearchStateData, ProductData> searchPageData = null;
    Future<ProductSearchPageData<SearchStateData, ProductData>> withWheelColor= null;
    Future<ProductSearchPageData<SearchStateData, ProductData>> withoutWheelColor = null;
    final  LinkedHashMap<String, String> sortedParams = getSortedParams(form.getParams(), form.getLastParam());
    final PageableData pageableData = createPageableData(0,
            getSearchPageSize(), null, ShowMode.Page);
    final SearchStateData searchState = new SearchStateData();
    searchState.setPdpFlow(Boolean.TRUE);
    final SearchQueryData searchQueryData = new SearchQueryData();
    String productCode = null;

    try {
        Callable<ProductSearchPageData<SearchStateData, ProductData>> withWheelColorThread = new DTCallable<ProductSearchPageData<SearchStateData, ProductData>>(
                null) {
            @Override
            public ProductSearchPageData<SearchStateData, ProductData> callMe(
                    String pk) throws Exception {
                buildWithParams(sortedParams, searchState,
                        searchQueryData, form);
                return productSearchFacade.textSearch(searchState,
                        pageableData);
            }
        };
        withWheelColor = executor.submit(withWheelColorThread);

        if (sortedParams.containsKey(WheelProductModel.WHEELCOLOR)
                && sortedParams.size() > 1) {
            Callable<ProductSearchPageData<SearchStateData, ProductData>> withoutWheelColorThread = new DTCallable<ProductSearchPageData<SearchStateData, ProductData>>(
                    null) {
                @Override
                public ProductSearchPageData<SearchStateData, ProductData> callMe(
                        String pk) throws Exception {
                    sortedParams.remove(WheelProductModel.WHEELCOLOR);//Call without wheelColor
                    buildWithParams(sortedParams, searchState,
                            searchQueryData, form);
                    return productSearchFacade.textSearch(searchState,
                            pageableData);
                }
            };
            withoutWheelColor = executor.submit(withoutWheelColorThread);
        }

        executor.shutdown();
        if (!executor.awaitTermination(120, TimeUnit.SECONDS)) {
            if(LOG.isDebugEnabled()) {
                LOG.debug("No Result Found!");
            }
        }
        if (null != withoutWheelColor
                && null != withoutWheelColor.get()
                && CollectionUtils.isNotEmpty(withoutWheelColor.get()
                        .getFacets())) {
            for (final FacetData<SearchStateData> facet : withoutWheelColor
                    .get().getFacets()) {
                if (null != facet.getCode() && facet.getCode()
                        .equals(WheelProductModel.WHEELCOLOR)) {
                    result.put("availableColors", facet.getValues());
                }
            }
        }
        searchPageData = withWheelColor.get();
    } catch (final Exception e) {
        LOG.error("Error getting PDP results : " + e.getMessage());
    }
    processResult(request, result, searchPageData, productCode);
 return result;

仅在搜索页面数据中使用withWheelColor,而在不使用WheelColor的情况下。上面为防止交换丢失了什么?

0 个答案:

没有答案