gpt4 book ai didi

java - 使用 java.text.Collat​​or 并发排序时如何避免线程阻塞?

转载 作者:行者123 更新时间:2023-11-30 03:00:29 26 4
gpt4 key购买 nike

当我的服务器负载过重时,我在 java.text.RuleBasedCollat​​or.getCollat​​ionKey() 中遇到线程阻塞问题。

这是出现问题的类:

public class DefaultFacetDisplayNameComparator implements Comparator
{
private final Collator trTRCollator = Collator.getInstance(new Locale("tr", "TR"));

@Override
public int compare(final FacetValue value1, final FacetValue value2)
{
if (!StringUtils.isEmpty(value1.getDisplayName()) && !StringUtils.isEmpty(value2.getDisplayName()))
{
//KOTT-672
final CollationKey displayName1 = trTRCollator.getCollationKey(value1.getDisplayName());
final CollationKey displayName2 = trTRCollator.getCollationKey(value2.getDisplayName());
return displayName1.compareTo(displayName2);
}
else if (!StringUtils.isEmpty(value1.getName()) && !StringUtils.isEmpty(value2.getName()))
{
//KOTT-672
final CollationKey name1 = trTRCollator.getCollationKey(value1.getName());
final CollationKey name2 = trTRCollator.getCollationKey(value2.getName());
return name1.compareTo(name2);
}
else
{
return 0;
}
}

这是线程转储的一个片段,显示了有问题的阻塞:

"xxxHTTP227" prio=5 tid=0x355 nid=0x163 BLOCKED owned by hybrisHTTP109 Id=236 - stats: cpu=339 blk=-1 wait=-1 java.lang.Thread.State: BLOCKED at java.text.RuleBasedCollator.getCollationKey(RuleBasedCollator.java:597) - waiting to lock java.text.RuleBasedCollator@1b9f75ac at com.core.commercesearch.search.solrfacetsearch.comparators.DefaultFacetDisplayNameComparator.compare(DefaultFacetDisplayNameComparator.java:27) at de.hybris.platform.solrfacetsearch.search.impl.comparators.FacetDisplayNameComparator.compare(FacetDisplayNameComparator.java:1) at java.util.TimSort.binarySort(TimSort.java:265) at java.util.TimSort.sort(TimSort.java:190) at java.util.TimSort.sort(TimSort.java:173) at java.util.Arrays.sort(Arrays.java:659) at java.util.Collections.sort(Collections.java:217) at de.hybris.platform.solrfacetsearch.search.impl.DefaultFacetSearchService.sortFacetValues(DefaultFacetSearchService.java:444) at de.hybris.platform.solrfacetsearch.search.impl.DefaultFacetSearchService.convertResponse(DefaultFacetSearchService.java:268) at de.hybris.platform.solrfacetsearch.search.impl.DefaultFacetSearchService.queryInternal(DefaultFacetSearchService.java:194) at de.hybris.platform.solrfacetsearch.search.impl.DefaultFacetSearchService.search(DefaultFacetSearchService.java:125) at de.hybris.platform.commerceservices.search.solrfacetsearch.populators.SolrSearchRequestResponsePopulator.populate(SolrSearchRequestResponsePopulator.java:68) at de.hybris.platform.commerceservices.search.solrfacetsearch.populators.SolrSearchRequestResponsePopulator.populate(SolrSearchRequestResponsePopulator.java:1) at de.hybris.platform.converters.impl.AbstractPopulatingConverter.populate(AbstractPopulatingConverter.java:61) at de.hybris.platform.commerceservices.converter.impl.AbstractPopulatingConverter.convert(AbstractPopulatingConverter.java:40) at de.hybris.platform.commerceservices.search.solrfacetsearch.impl.DefaultSolrProductSearchService.doSearch(DefaultSolrProductSearchService.java:127) at de.hybris.platform.commerceservices.search.solrfacetsearch.impl.DefaultSolrProductSearchService.searchAgain(DefaultSolrProductSearchService.java:111) at de.hybris.platform.commerceservices.search.solrfacetsearch.impl.DefaultSolrProductSearchService.searchAgain(DefaultSolrProductSearchService.java:1) at de.hybris.platform.commercefacades.search.solrfacetsearch.impl.DefaultSolrProductSearchFacade$4.execute(DefaultSolrProductSearchFacade.java:184) at de.hybris.platform.commercefacades.search.solrfacetsearch.impl.DefaultSolrProductSearchFacade$4.execute(DefaultSolrProductSearchFacade.java:1) at de.hybris.platform.commerceservices.threadcontext.impl.DefaultThreadContextService.executeInContext(DefaultThreadContextService.java:54) at de.hybris.platform.commercefacades.search.solrfacetsearch.impl.DefaultSolrProductSearchFacade.categorySearch(DefaultSolrProductSearchFacade.java:177) at com.acme.storefront.controllers.pages.CategoryPageController$CategorySearchEvaluator.doSearch(CategoryPageController.java:918)

当线程转储显示服务器线程正在执行的操作时,如何避免它们被阻塞?

最佳答案

我不会猜测任何 Collat​​orgetCollat​​ionKey() 实现都需要同步,但这就是线程转储似乎显示的内容。从问题中尚不清楚,但我倾向于认为,虽然当多个线程共享 Collat​​or 时这可能会减慢您的应用程序,但它不会使它们死锁。

但是,无论哪种方式,最好的解决方案可能是每个线程使用自己的 Collat​​or 实例,而最好的方法可能是每个线程都有自己的 Collat​​or 实例。主机类,DefaultFacetDisplayNameComparator

这确实假设 Collat​​or.getInstance() 每次调用时都会返回不同的 Collat​​or 实例。如果您发现它在不同线程的不同调用中返回相同的实例,并且该实例存在同步问题,那么首先,提交错误报告;其次,使用不同的技术来获取您的Collat​​or(可能直接构建适当的RuleBasedCollat​​or)。

关于java - 使用 java.text.Collat​​or 并发排序时如何避免线程阻塞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36135914/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com