# Error troubleshooting java thread pool
# Device
- arm64 macOS
# Environment
- Java corretto-17
- Springboot 3.0.2
# Problem
- Problem description
I have a problem when usingSpringboot
ThreadPoolTaskExecutor
. The code below looks like this:
// create operation | |
var finalGroup = group; | |
var path = getGroupParentPath(finalGroup.getId()); | |
var sourcePath = finalGroup.getSourceId()+"/"+path; | |
var event = new ChokidarEvent().setEvent(ChangeType.addDir).setPath( | |
sourcePath | |
); | |
threadPoolTaskExecutor.submit(() -> { | |
sendChokidarEvent(finalGroup.getSourceId(), event); | |
}); |
- Porblem reproduce
ThesendChokidarEvent
create function is blocking when the code waits for the completion of the get function on theFuture
. But while another delete function is running, the code is continuing to run.
threadPoolTaskExecutor.submit(new Thread(()->{ | |
if (subscribers.containsKey(finalGroup.getSourceId())) { | |
var path = getGroupParentPath(finalGroup.getParent()); | |
var sourcePath = finalGroups.get(0).getSourceId() + "/" + path + "/" + id; | |
if (!StringUtils.hasText(path)){ | |
sourcePath = finalGroups.get(0).getSourceId() + "/" + id; | |
} | |
sendChokidarEvent(finalGroup.getSourceId(), new ChokidarEvent().setEvent(ChangeType.unlinkDir).setPath(sourcePath)); | |
} | |
})); |
- Test result
When testing with POSTMAN, it can be observed that theunlinkDir
event is being sent before theaddDir
event in the output of the SseEmitter.
# Knowledge
ThreadPoolTaskExecutor
utilizes a blocking queue approach, and due to a certain condition, the addDir
function sendChokidarEvent
is continuously blocked.
# Solution
- No similar issue has been found in the existing online resources.
- Inspecting the source code.
After examining the source code, it becomes evident that ThreadPoolTaskExecutor
is merely a wrapper around ThreadPoolExecutor
. Furthermore, upon inspecting ThreadPoolExecutor
, it can be observed that there is no actively blocking code within it. However, after setting another breakpoint and further analysis, it was discovered that the reason for breakpoints disappearing in subsequent code segments is due to the getGroupParentPath
function.
# Solved
This issue is a basic mistake, caused by a typo in the variable within getGroupParentPath
, resulting in an infinite loop and preventing the execution of subsequent code.