This article mainly introduces the wonderful use of Future in Java multi-thread processing (source code attached). It is quite good. Friends in need can refer to it.
Future in java is a future object, which stores the thread processing results. It is like a delivery voucher. With it, you can extract the results at any time. In both cases, leaving Future is almost impossible. One situation is to split the order. For example, your application receives a batch order. At this time, if you require the fastest processing of the order, you need to process it concurrently. If the concurrent results are collected, this problem will be very cumbersome if you program it yourself. You can use CompletionService to solve this problem. CompletionService collects Future into a queue and can enter the queue in the order in which the results are processed. Another situation is that if you need to query something concurrently (such as a crawler), as long as one result of the concurrent query is returned, you will think that the query has been reached and end the query. In this case, you also need to use CompletionService and Future to solve the problem. It is more intuitive to enter the code directly:
import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.concurrent.Callable; import java.util.concurrent.CompletionService; import java.util.concurrent.ExecutorCompletionService; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class CompletionServiceTest { static int numThread =100; static ExecutorService executor = Executors.newFixedThreadPool(numThread); public static void main(String[] args) throws Exception{ //data表示批量任务 int[] data =new int[100]; for(int i=1;i<100000;i++){ int idx =i % 100; data[idx] =i; if(i%100==0){ testCompletionService(data); data =new int[100]; } } } private static void testCompletionService(int [] data) throws Exception{ CompletionService<Object> ecs = new ExecutorCompletionService<Object>(executor); for(int i=0;i<data.length;i++){ final Integer t=data[i]; ecs.submit(new Callable<Object>() { public Object call() { try { Thread.sleep(new Random().nextInt(1000)); } catch (InterruptedException e) { e.printStackTrace(); } return t; } }); } //CompletionService会按处理完后顺序返回结果 List<Object> res =new ArrayList<Object>(); for(int i = 0;i<data.length;i++ ){ Future<Object> f = ecs.take(); res.add(f.get()); } System.out.println(Thread.currentThread().getName()+":"+res); } private static void testBasicFuture(int [] data) throws Exception{ List<Future<Object>> res =new ArrayList<Future<Object>>(); for(int i=0;i<data.length;i++){ final Integer t=data[i]; Future<Object> future=executor.submit(new Callable<Object>() { public Object call() { return t; } }); res.add(future); } for(int i = 0;i<res.size();i++ ){ Future<Object> f = res.get(i); Object rObject =f.get(); System.out.print(":"+rObject); } System.out.println("LN"); } }
Summary
The above is the detailed content of Examples of the wonderful use of Future in Java multi-thread processing. For more information, please follow other related articles on the PHP Chinese website!