Java ExecutorCompletIonservice.

时间:2020-02-23 14:35:00  来源:igfitidea点击:

在本教程中,我们将看到Java ExecutorCompletIonsEvice示例。 ExecutorCompletionService类工具 CompletionService
此类以完成顺序返回将来对象。

为什么我们可能需要使用executorcompletionservice:

让我们在方案的帮助下了解:

假设我们有5个任务,我们将其提交给执行者,并在任务完成后立即执行一些操作。

我们还假设第一个任务需要最长的时间。
如果在这种情况下使用executors,请在我们翻译将来。

获取()在第一个任务中,Get操作将被阻止,即使其他任务可能已完成,我们将无法进一步进行。

要解决此问题,我们可以使用 ExecutorCompletionServiceExecutorCompletionService基于完成顺序返回期货对象,从而首先返回哪个任务,首先返回。
你只需要调用 executorCompletionService.take()完成未来的对象。

Java ExecutorCompletIonservice示例:

让我们创建一个非常简单的例子。

步骤1:创建名为"multiplyingtask.java"的可调用任务。

package org.igi.theitroad;
import java.util.concurrent.Callable;
 
public class MultiplyingTask implements Callable{
	int a;
	int b;
	long sleepTime;
	String taskName;
 
	public MultiplyingTask(String taskName,int a, int b, long sleepTime) {
		this.taskName=taskName;
		this.a=a;
		this.b=b;
		this.sleepTime=sleepTime;
	}
 
	@Override
	public Integer call() throws Exception {
		System.out.println("Started taskName: "+taskName);
		int result=a*b;
		Thread.sleep(sleepTime);
		System.out.println("Completed taskName: "+taskName);
		return result;
	}
}

第2步:创建一个名为"futuretaskmain"的类。
这将是我们的主班。

package org.igi.theitroad;
 
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
 
public class ExecutionCompletionServiceaMain {
 
	public static void main(String[] args) {
		MultiplyingTask multiplyingTask1= new MultiplyingTask("Task 1",10,20,2000l);
		MultiplyingTask multiplyingTask2= new MultiplyingTask("Task 2",30,40,4000l);
		MultiplyingTask multiplyingTask3= new MultiplyingTask("Task 3",40,50,3000l);
		MultiplyingTask multiplyingTask4= new MultiplyingTask("Task 4",50,60,1000l);
		
		ExecutorService executorService = Executors.newFixedThreadPool(4);
		CompletionService<Integer> executorCompletionService= new ExecutorCompletionService<>(executorService);
		List<Future<Integer>> futures = new ArrayList<Future<Integer>>();
		futures.add(executorCompletionService.submit(multiplyingTask1));
		futures.add(executorCompletionService.submit(multiplyingTask2));
		futures.add(executorCompletionService.submit(multiplyingTask3));
		futures.add(executorCompletionService.submit(multiplyingTask4));
 
		for (int i=0; i<futures.size(); i++) {
		    try {
				Integer result = executorCompletionService.take().get();
				System.out.println("Result: " +result);
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (ExecutionException e) {
				e.printStackTrace();
			}
		}
		executorService.shutdown();
	}
}

让我们运行上面的程序来检查输出:

Started taskName: Task 1
Started taskName: Task 3
Started taskName: Task 2
Started taskName: Task 4
Completed taskName: Task 4
Result: 3000
Completed taskName: Task 1
Result: 200
Completed taskName: Task 3
Result: 2000
Completed taskName: Task 2
Result: 1200

正如我们可以使用输出所看到的,所有四个任务都开始谨慎,但我们根据完成顺序检索结果 executorCompletionService.take().get()而不是调用 get()未来对象的方法。