This article mainly explores the use of Java concurrent programming callable and future, and shares relevant example codes, and details are as follows.
We all know that there are two ways to implement multi-threading. One is to inherit Thread and the other is to implement Runnable. However, both of these two ways have a defect, and the return result cannot be obtained after the task is completed. To get the return result, you have to use Callable. The Callable task can have a return value, but it cannot directly obtain the return value from the Callable task; if you want to get the return value of the Callable task, you need to use Future. So Callable tasks and Future mode are usually used in combination.
Imagine a scenario: you need a post list interface. In addition to returning the post list, you also need to return the like list and comment list of each post. For calculations of 10 posts on a page, this interface requires access to the database 21 times. Once accessing the database is calculated based on 100ms, 21 times, and the cumulative time is 2.1s. This response time is probably unsatisfactory. What to do? Asynchronous transformation of interfaces.
After finding out the post list, iterate over the post list, start 10 threads in the loop, and concurrently obtain the like list of each post, and at the same time, 10 threads are set up to obtain the comment list of each post. After this transformation, the response time of the interface is greatly shortened to 200ms. At this time, we need to use Callabel combined with Future to achieve it.
private List<PostResponse> createPostResponseList(Page<PostResponse> page,final String userId){ if(page.getCount()==0||page==null||page.getList()==null){ return null; } //Get the post list 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); } } // Get 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; }Summarize
The above is the entire content of this article about the application example code of Java concurrent programming Callable and Future. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!