Java concurrent programming Callable and Future application instance code
- 2020-12-20 03:35:37
- OfStack
This paper mainly explores the use of java and callable for concurrent programming, and shares the relevant example code, which is described as follows.
We all know that there are two ways to implement multithreading, one is to inherit from Thread, and the other is to implement Runnable, but both of these ways have a defect that they cannot get the returned result after the task is completed. To get the return result, you need to use Callable. The Callable task can have the return value, but it cannot get the return value directly from the Callable task. To get the return value of the Callabel task, you need Future. So the Callable task and the Future pattern are often used together.
Consider a scenario where you need a list of posts interface, and in addition to returning a list of posts, you also need to return a thumb up list and a list of comments for each post. To calculate 10 posts on a page, this interface needs to access the database 21 times, and to access the database once is calculated as 100ms, 21 times, and the cumulative time is 2.1s. This response time may not be satisfactory. What to do? Asynchronous transformation interface.
After finding the list of posts, the list of posts is iterated, and 10 threads are set up in the loop to get the thumb up list of each post concurrently, and another 10 threads are set up to get the comment list of each post concurrently. After this modification, the response time of the interface is greatly reduced at 200ms. At this time, Callabel combined with Future to achieve.
private List<PostResponse> createPostResponseList(Page<PostResponse> page,final String userId){
if(page.getCount()==0||page==null||page.getList()==null){
return null;
}
// Get a list of posts
List<PostResponse> circleResponseList = page.getList();
int size=circleResponseList.size();
ExecutorService commentPool = Executors.newFixedThreadPool(size);
ExecutorService supportPool = Executors.newFixedThreadPool(size);
try {
List<Future> commentFutureList = new ArrayList<Future>(size);
if (circleResponseList != null && circleResponseList.size() > 0) {
for (PostResponse postResponse : circleResponseList) {
final String circleId=postResponse.getId();
final String postUserId=postResponse.getUserId();
// Check the comment list
Callable<List<CircleReviews>> callableComment = new Callable<List<CircleReviews>>() {
@Override
public List<CircleReviews> call() throws Exception {
return circleReviewsBiz.getPostComments(circleId);
}
};
Future f = commentPool.submit(callableComment);
commentFutureList.add(f);
// Check the like list
Callable<List<CircleZan>> callableSupport = new Callable<List<CircleZan>>() {
@Override
public List<CircleZan> call() throws Exception {
return circleZanBiz.findList(circleId);
}
};
Future supportFuture = supportPool.submit(callableSupport);
commentFutureList.add(supportFuture);
}
}
// Gets the execution results of all concurrent tasks
int i = 0;
PostResponse temp = null;
for (Future f : commentFutureList) {
temp = circleResponseList.get(i);
temp.setCommentList((List<CircleReviews>) f.get();
temp.setSupportList((List<CircleZan>) f.get();
circleResponseList.set(i, temp);
i++;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// Close the thread pool
commentPool.shutdown();
supportPool.shutdown();
}
return circleResponseList;
}
conclusion
That's the end of this article on concurrent programming Java Callable and Future application example code, I hope to help you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!