gpt4 book ai didi

multithreading - 线程停在AbstractFuture上的等待条件下

转载 作者:行者123 更新时间:2023-12-03 13:04:34 24 4
gpt4 key购买 nike

因此我们正在使用Guava v18。
我们将对此内容进行大量讨论:

"catalina-exec-ajp2" daemon prio=10 tid=0x00007fb45c001800 nid=0x18aa waiting on condition [0x00007fb435151000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x000000075ff0cfe8> (a com.google.common.util.concurrent.AbstractFuture$Sync)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:834)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(AbstractQueuedSynchronizer.java:994)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1303)
at com.google.common.util.concurrent.AbstractFuture$Sync.get(AbstractFuture.java:285)
at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:116)
at com.google.common.util.concurrent.Uninterruptibles.getUninterruptibly(Uninterruptibles.java:137)
at com.google.common.cache.LocalCache$LoadingValueReference.waitForValue(LocalCache.java:3557)
at com.google.common.cache.LocalCache$Segment.waitForLoadingValue(LocalCache.java:2302)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2191)
at com.google.common.cache.LocalCache.get(LocalCache.java:3937)
at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3941)
at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4824)
at com.google.common.cache.LocalCache$LocalLoadingCache.getUnchecked(LocalCache.java:4830)
at com.xxx.yyy.repository.cache.acl.guava.GuavaCacheWrapper.get(GuavaCacheWrapper.java:21)
at com.xxx.yyy.repository.cache.CachedCpaRepository.findAll(CachedCpaRepository.java:32)

我们的 Guava 配置为:
  @SuppressWarnings("unchecked")
@Override
protected <K, R> CacheWrapper<K, R> newCacheConfiguration(Fetcher fetcher, String cacheName, int maxElementsInMemory,
int timeToIdleSeconds, int timeToLiveSeconds)
{
Cache existingCache = manager.getCache(cacheName);
CacheWrapper<K, R> cache;
if(null == existingCache)
{
cache = new GuavaCacheWrapper<>(CacheBuilder.newBuilder()
.maximumSize(maxElementsInMemory)
.expireAfterAccess(timeToIdleSeconds, SECONDS)
.refreshAfterWrite(timeToLiveSeconds, SECONDS)
.recordStats()
.build( CacheLoader.asyncReloading(fetcher.getAs(CacheLoader.class), executors)));
manager.addCache(cacheName, cache);
}
else
{
cache = (CacheWrapper<K, R>) ((GuavaCache) existingCache).getNativaGuavaCache();
}
return cache;
}

我对可以检查出导致问题的原因一无所知。您能提出我可以检查的内容吗?

最佳答案

如果您在LoadingCache上遇到阻塞,则意味着您正在从多个线程中加载同一键的值:第一个线程实际加载了该值,而下一个线程等待该加载完成。

也许您的 key 数量很少,并且处理该值是一项昂贵的操作?您可以记录您要加载的 key 以及加载的持续时间。

更新

实际加载的线程是调用CacheLoader的那些线程,但是您无法从线程转储中找到哪个线程与等待线程相对应(如果有多个加载的线程)。

我创建了一个测试来重现这种情况:

public class ExpensiveCache {
private final LoadingCache<String, String> cache = CacheBuilder.newBuilder().build(new ExpensiveCacheLoader());

public static void main(String... args) {
new ExpensiveCache().run();
}

private void run() {
// Start 2 threads concurrently accessing the same key in the cache
for (int i = 0; i < 2; i++) {
new Thread(() -> cache.getUnchecked("key")).start();
}
}

private static class ExpensiveCacheLoader extends CacheLoader<String, String> {
@Override
public String load(String key) throws Exception {
// Make the computation "expensive" by sleeping for 60s
Thread.sleep(60_000);
return key;
}
}
}

您确实在线程转储的其他任何地方都找不到条件的ID:
"Thread-2" #12 prio=5 os_prio=31 tid=0x00007fbb6384e000 nid=0x5703 waiting on condition [0x00000001291c5000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x000000076b183208> (a com.google.common.util.concurrent.AbstractFuture$Sync)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(AbstractQueuedSynchronizer.java:997)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1304)
at com.google.common.util.concurrent.AbstractFuture$Sync.get(AbstractFuture.java:285)
at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:116)
at com.google.common.util.concurrent.Uninterruptibles.getUninterruptibly(Uninterruptibles.java:137)
at com.google.common.cache.LocalCache$LoadingValueReference.waitForValue(LocalCache.java:3557)
at com.google.common.cache.LocalCache$Segment.waitForLoadingValue(LocalCache.java:2302)
at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2289)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2197)
at com.google.common.cache.LocalCache.get(LocalCache.java:3937)
at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3941)
at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4824)
at com.google.common.cache.LocalCache$LocalLoadingCache.getUnchecked(LocalCache.java:4830)
at io.github.fpavageau.ExpensiveCache.lambda$run$0(ExpensiveCache.java:16)
at io.github.fpavageau.ExpensiveCache$$Lambda$1/1108411398.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)

"Thread-1" #11 prio=5 os_prio=31 tid=0x00007fbb6384d000 nid=0x5503 waiting on condition [0x00000001290c2000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at io.github.fpavageau.ExpensiveCache$ExpensiveCacheLoader.load(ExpensiveCache.java:23)
at io.github.fpavageau.ExpensiveCache$ExpensiveCacheLoader.load(ExpensiveCache.java:20)
at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3527)
at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2319)
at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2282)
- locked <0x000000076b199a50> (a com.google.common.cache.LocalCache$StrongEntry)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2197)
at com.google.common.cache.LocalCache.get(LocalCache.java:3937)
at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3941)
at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4824)
at com.google.common.cache.LocalCache$LocalLoadingCache.getUnchecked(LocalCache.java:4830)
at io.github.fpavageau.ExpensiveCache.lambda$run$0(ExpensiveCache.java:16)
at io.github.fpavageau.ExpensiveCache$$Lambda$1/1108411398.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)

关于multithreading - 线程停在AbstractFuture上的等待条件下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32610152/

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