package main.java;
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
/**
* 批量任务的限时 invokeAll(tasks) 批量提交不限时任务
*
* invokeAll(tasks, timeout, unit) 批量提交限时任务
*
* InvokeAll方法处理一个任务的容器(collection),并返回一个Future的容器。两个容器具有相同的结构:
* invokeAll将Future添加到返回的容器中,这样可以使用任务容器的迭代器,从而调用者可以将它表现的Callable与Future 关联起来。
* 当所有任务都完成时、调用线程被中断时或者超过时限时,限时版本的invokeAll都会返回结果。 超过时限后,任务尚未完成的任务都会被取消。
*
*
*
*/
public class InvokeAllThread {
static ExecutorService mExecutor = Executors.newCachedThreadPool();
/**
* 计算价格的任务
*
* @author hadoop
*
*/
private class QuoteTask implements Callable<Integer> {
public final double price;
public final int num;
public QuoteTask(double price, int num) {
this.price = price;
this.num = num;
}
public Integer call() throws Exception {
Random r = new Random();
long time = (r.nextInt(10) + 1) * 1000;
Thread.sleep(time);
Integer d = Integer.valueOf((int) (price * num));
System.out.println("wait:" + time / 1000 + "s,price:" + price + ",num:" + num + ",total:" + d);
return d;
}
}
/**
* 在预定时间内请求获得旅游报价信息
*
* @return
*/
public void getQueueTasks() throws InterruptedException {
int sum = 0 ;
List<QuoteTask> tasks = new ArrayList<QuoteTask>();
// 模拟5个计算旅游报价的任务
for (int i = 1; i <= 5; i++) {
tasks.add(new QuoteTask(200, i));
}
/**
* 使用invokeAll方法批量提交限时任务任务
*
*/
List<Future<Integer>> futures = mExecutor.invokeAll(tasks);
// 报价合计集合
List<Integer> totalPriceList = new ArrayList<Integer>();
for (Future<Integer> future : futures) {
try {
totalPriceList.add(future.get());
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for (Integer Integer : totalPriceList) {
sum = sum+Integer;
}
System.out.println("总价:"+sum);
mExecutor.shutdown();
}
public static void main(String[] args) {
try {
InvokeAllThread it = new InvokeAllThread();
it.getQueueTasks();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
/**
* 批量任务的限时 invokeAll(tasks) 批量提交不限时任务
*
* invokeAll(tasks, timeout, unit) 批量提交限时任务
*
* InvokeAll方法处理一个任务的容器(collection),并返回一个Future的容器。两个容器具有相同的结构:
* invokeAll将Future添加到返回的容器中,这样可以使用任务容器的迭代器,从而调用者可以将它表现的Callable与Future 关联起来。
* 当所有任务都完成时、调用线程被中断时或者超过时限时,限时版本的invokeAll都会返回结果。 超过时限后,任务尚未完成的任务都会被取消。
*
*
*
*/
public class InvokeAllThread {
static ExecutorService mExecutor = Executors.newCachedThreadPool();
/**
* 计算价格的任务
*
* @author hadoop
*
*/
private class QuoteTask implements Callable<Integer> {
public final double price;
public final int num;
public QuoteTask(double price, int num) {
this.price = price;
this.num = num;
}
public Integer call() throws Exception {
Random r = new Random();
long time = (r.nextInt(10) + 1) * 1000;
Thread.sleep(time);
Integer d = Integer.valueOf((int) (price * num));
System.out.println("wait:" + time / 1000 + "s,price:" + price + ",num:" + num + ",total:" + d);
return d;
}
}
/**
* 在预定时间内请求获得旅游报价信息
*
* @return
*/
public void getQueueTasks() throws InterruptedException {
int sum = 0 ;
List<QuoteTask> tasks = new ArrayList<QuoteTask>();
// 模拟5个计算旅游报价的任务
for (int i = 1; i <= 5; i++) {
tasks.add(new QuoteTask(200, i));
}
/**
* 使用invokeAll方法批量提交限时任务任务
*
*/
List<Future<Integer>> futures = mExecutor.invokeAll(tasks);
// 报价合计集合
List<Integer> totalPriceList = new ArrayList<Integer>();
for (Future<Integer> future : futures) {
try {
totalPriceList.add(future.get());
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for (Integer Integer : totalPriceList) {
sum = sum+Integer;
}
System.out.println("总价:"+sum);
mExecutor.shutdown();
}
public static void main(String[] args) {
try {
InvokeAllThread it = new InvokeAllThread();
it.getQueueTasks();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}