Use FutureTask and Callable with Multithreading to Boost Your Java Application Performance
Here I have a sample Spring Boot Web application to get user information. Essentially there are three classes that matter:
UserController.java
This is a RestController class which has a rest endpoint “/GetUserInfo” with RequestParam “userId”. This will return the user information of the particular userId.
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/GetUserInfo")
public Object getUserInfo(@RequestParam("userId")long userId){
return userService.getUserInfo(userId);
}
}
UserService.java
UserService contains business logic to fetch the necessary user information. Here, for demo purposes, I am getting userName, userMoney and userCountry. Each information is fetched and passed as a JSONObject. Then we combine all the pieces into one JSONObject result and return it. At the end of the program, the total time consumed for getting user information is printed.
@Service
public class UserService {
@Autowired
private RemoteService remoteService;
public Object getUserInfo(long userId) {
long currentTimeMillis = System.currentTimeMillis();
//1. Use userName…