Issue
all. I'm new to SpringBoot. And now I hope to naming a thread based on the parameter.
Take a simple example, I'm currently using @Async
notation to create an Asynchronous Method
@PostMapping
public void requestMethod(@RequestBody String id){
...
asyncMethod(id);
}
@Async
public void asyncMethod(String id){
...
}
Once there is a POST
request send to requestMethod
, the method will create an asynchronous method in a new thread. The name of the thread is the same as the parameter received as id
.
Because there will be many asynchronous methods in the program. And the asynchronous method will run within in an infinite loop. So, I want to terminate the threads based on their name. But how can I do that?
If there is any ambiguous, please let me know. Thanks in advance.
Solution
The easiest way is just to set thread name by yourself
@Async
public void asyncMethod(String id){
Thread.currentThread().setName("thread-" + id);
...
}
Answered By - 刷题养家
Answer Checked By - Katrina (JavaFixing Volunteer)