提问人:Kerwin Bryant 提问时间:11/10/2023 最后编辑:user207421Kerwin Bryant 更新时间:11/10/2023 访问量:58
Java 线程卡住
Java thread stuck
问:
代码为:
//=========
//classA
//=========
private static final int CORE_POOL_SIZE = Math.max(1, Runtime.getRuntime().availableProcessors() / 2);
private static final int MAX_POOL_SIZE = CORE_POOL_SIZE + 1;
private static final int KEEP_ALIVE_TIME = 60;
private static final int MAX_QUEUE_SIZE = 100000;
private ThreadPoolExecutor pool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(MAX_QUEUE_SIZE));
public void addTask(final String md5) {
pool.submit(() -> {
try {
logger.info("a1");
doAction(md5);
logger.info("a2");
} catch (Exception e) {
logger.error("a3");
}
});
}
private void doAction(final String md5) throws Exception {
logger.error("b1");
saveFile(getFileByMd5(md5), md5);
logger.error("b2");
}
public void saveFile(final File file, final String md5) throws Exception {
synchronized (md5.intern()) {
log.info("c1");
FileAttUtils.parseFileInfo(file);
log.info("c2");
}
}
//=========
//classB
//=========
public class FileAttUtils {
public static Map<String, Object> parseFileInfo(File file) {
log.info("d1");
SimpleImageInfo image = new SimpleImageInfo(file.getAbsolutePath());
log.info("d2");
}
}
//=========
//classC
//=========
public class SimpleImageInfo {
public SimpleImageInfo(String path) {
String suffix = getFileSuffix(path);
log.info("e1");
InputStream is = null;
try {
...
} catch (IOException e) {
e.printStackTrace();
} finally {
...
}
}
private static String getFileSuffix(String path) {
String result = null;
if (path != null) {
result = "";
if (path.lastIndexOf('.') != -1) {
result = path.substring(path.lastIndexOf('.'));
if (result.startsWith("."))
result = result.substring(1);
}
}
log.info("getFileSuffix: {}", result);
return result;
}
}
在程序中调用时,将显示以下内容:addTask
log
[pool-2-thread-1] xxxxx: a1
[pool-2-thread-2] xxxxx: a1
[pool-2-thread-1] xxxxx: b1
[pool-2-thread-2] xxxxx: b1
[pool-2-thread-1] xxxxx: c1
[pool-2-thread-2] xxxxx: c1
[pool-2-thread-1] xxxxx: d1
[pool-2-thread-2] xxxxx: d1
该程序似乎卡在这一行:
SimpleImageInfo image = new SimpleImageInfo(file.getAbsolutePath());
但从日志来看,程序似乎根本没有进入(因为没有打印)SimpleImageInfo#constructor
e1
我观察到以下两条线程消息:jstack
"pool-2-thread-2" #56 prio=5 os_prio=0 tid=0x00007fb6dc003000 nid=0x4e54 waiting on condition [0x00007fb754fe4000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000006c1931ae8> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
Locked ownable synchronizers:
- None
"pool-2-thread-1" #55 prio=5 os_prio=0 tid=0x00007fb6dc002000 nid=0x4e53 waiting on condition [0x00007fb75fe51000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000006c1931ae8> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
Locked ownable synchronizers:
- None
我搜索了 的所有输出,但我找不到线程(停车等待<0x00000006c1931ae8>)。jstack
0x00000006c1931ae8
我想知道为什么我的程序卡住了?为什么线程中的代码在执行中途进入等待状态?
答: 暂无答案
评论