1.Callable
1) Runnable, which performs an independent task but does not return a value. If you want a return value after the task is completed, you can use the callable interface;
2) Callable is a paradigm with type parameters. Its type parameter method is expressed as the value returned by the method call () instead of run (), and it must be used ExecutorService.submint () method.
difference:
1. Callable, which accepts a generic type and returns a value of this type in the call() method; however, runnable’s run() method has no return value;
2. Callable, whose call() method can throw an exception, while runnable’s run() method does not.
2. Business scenario:
In HTTP requests, the business processing process takes a long time, such as large queries, remote calls and other scenarios. The main thread that receives HTTP requests will be occupied all the time, and the number of threads in Tomcat thread pool is limited. Therefore, the number of HTTP requests accepted per unit time will decrease.
3. Code examples
1)controller
package com.liuxd.controller;
import com.liuxd.entity.Responses;
import com.liuxd.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.Callable;
@Slf4j
@RestController
public class AsyncCallableController {
@Autowired
private BusinessService businessService;
@GetMapping(value = "/getData")
public Callable<Responses<String>> getData() {
log.info("HTTP request received...");
Callable<Responses<String>> data = (() -> {
return businessService.getData();
});
log.info("The task of the receiving HTTP request thread has been completed, exit!");
return data;
}
}
2) service
package com.liuxd.service;
import com.liuxd.entity.Responses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class BusinessService {
public Responses<String> getData(){
log.info("Call the service method and start the execution...");
try {
Thread.sleep(2500L);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("Call service method, end of execution!");
return new Responses<>(0,"Done","SUCCESS");
}
}
3) Print results
Read More:
- Asynchronous processing of HTTP request by Java_ Method 2: through deferredresult
- Spring boot uses thread pool to realize asynchronous processing without return and asynchronous processing with return
- After asynchronous file import and springboot multipartfile upload, the @async asynchronous processing reports an error: nosuchfileexception
- Request processing failed; nested exception is java.lang.NullPointerException or UnsatisfiedDependencyE
- Java: How to Find the Minimum Value from a Random Array by Bubble Sort Method
- Asynchronous callback case of Java callback function
- [Solved] “status“:405,“error“ Request method ‘POST‘ not supported“
- Abstract method and static method of java interface
- SpringBoot :Error parsing HTTP request header [How to Solve]
- [Solved] Upload Files Error: Request processing failed;nested exception is org.springframework.web.multipart.MultipartExcepti
- [Solved] Java.lang.IllegalStateException: getReader() has already been called for this request
- [Solved] Method threw ‘java.lang.StackOverflowError‘ exception. Cannot evaluate
- JAVA: How to use the third-party font library method (using the third-party TTF / TTC font library method)
- SpringCloud Use openFeign Multipartfile to Upload Files Error: Current request is not a multipart request
- [Solved] IDEA java Cannot resolve method ‘getName‘ in ***
- [Solved] JAVA HttpClient Send Https request certificate error:PKIX path building failed:
- [Solved] Jenkins error: Asynchronous execution failure
- [Solved] SpringCloud Compile Error: java: JPS incremental annotation processing is disabled. Compilation results on partial recompilation may be inaccurate.
- [Solved] Error: caused by: java.net.bindexception: the address is already in use
- [Solved] Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: