任务的批量提交invokeAll两种方法的区别

ExecutorService的invokeAll方法有两种用法:

1.exec.invokeAll(tasks)

2.exec.invokeAll(tasks, timeout, unit)

其中tasks是任务集合,timeout是超时时间,unit是时间单位

两者都会堵塞,必须等待所有的任务执行完成后统一返回,一方面内存持有的时间长;另一方面响应性也有一定的影响,毕竟大家都喜欢看看刷刷的执行结果输出,而不是苦苦的等待;

但是方法二增加了超时时间控制,这里的超时时间是针对的所有tasks,而不是单个task的超时时间。如果超时,会取消没有执行完的所有任务,并抛出超时异常。相当于将每一个future的执行情况用一个list集合保存,当调用future.get()方法取值时和设置的timeout比较,是否超时。

InvokeAll方法处理一个任务的容器(collection),并返回一个Future的容器。两个容器具有相同的结构;

这里提交的任务容器列表和返回的Future列表存在顺序对应的关系。

invokeAll将Future添加到返回容器中,这样可以使用任务容器的迭代器,从而调用者可以将它表现的Callable与Future关联起来。

当所有任务都完成时、调用线程被中断时或者超过时限时,限时版本的invokeAll都会返回结果。超过时限后,任何尚未完成的任务都会被取消。

作为invokeAll的返回值,每个任务要么正常地完成,要么被取消。


invokeAll控制批量任务的时间期限的例子:

package com.thread;

import java.math.BigDecimal;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Iterator;
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都会返回结果。 超过时限后,任务尚未完成的任务都会被取消。
 * 
 * @author hadoop
 *
 */
public class InvokeAllThread {
	// 固定大小的线程池,同时只能接受5个任务
	static ExecutorService mExecutor = Executors.newFixedThreadPool(5);

	/**
	 * 计算价格的任务
	 * @author hadoop
	 *
	 */
	private class QuoteTask implements Callable<BigDecimal> {
		public final double price;
		public final int num;

		public QuoteTask(double price, int num) {
			this.price = price;
			this.num = num;
		}

		@Override
		public BigDecimal call() throws Exception {
			Random r = new Random();
			long time = (r.nextInt(10) + 1) * 1000;
			Thread.sleep(time);

			BigDecimal d = BigDecimal.valueOf(price * num).setScale(2);
			System.out.println("耗时:" + time / 1000 + "s,单价是:" + price + ",人数是:"
					+ num + ",总额是:" + d);
			return d;
		}
	}

	/**
	 * 在预定时间内请求获得旅游报价信息
	 * 
	 * @return
	 */
	public   void getRankedTravelQuotes() throws InterruptedException {
		List<QuoteTask> tasks = new ArrayList<QuoteTask>();
		// 模拟10个计算旅游报价的任务
		for (int i = 1; i <= 20; i++) {
			tasks.add(new QuoteTask(200, i) );
		}

		/**
		 * 使用invokeAll方法批量提交限时任务任务 预期15s所有任务都执行完,没有执行完的任务会自动取消
		 * 
		 */
	    List<Future<BigDecimal>> futures = mExecutor.invokeAll(tasks, 15, TimeUnit.SECONDS);
		// 报价合计集合
		List<BigDecimal> totalPriceList = new ArrayList<BigDecimal>();

		Iterator<QuoteTask> taskIter = tasks.iterator();

		for (Future<BigDecimal> future : futures) {
			 QuoteTask task = taskIter.next();
			try {
				totalPriceList.add(future.get());
			} catch (ExecutionException e) {
				// 返回计算失败的原因
				// totalPriceList.add(task.getFailureQuote(e.getCause()));
				totalPriceList.add(BigDecimal.valueOf(-1));
				 System.out.println("任务执行异常,单价是"+task.price+",人数是:"+task.num);
			} catch (CancellationException e) {
				// totalPriceList.add(task.getTimeoutQuote(e));
				totalPriceList.add(BigDecimal.ZERO);
				 System.out.println("任务超时,取消计算,单价是"+task.price+",人数是:"+task.num);
			}
		}
		for (BigDecimal bigDecimal : totalPriceList) {
			System.out.println(bigDecimal);
		}
		mExecutor.shutdown();
	}

	
	public static void main(String[] args) {
		try {
			InvokeAllThread it = new InvokeAllThread();
			it.getRankedTravelQuotes();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

再来一个invokeAll不控制超时时间的小例子:

/**
 * 测试InvokeAll批量提交任务集
 * @throws InterruptedException 
 */
public  static void testInvokeAllThread() throws InterruptedException{
	ExecutorService exec = Executors.newFixedThreadPool(10);

	List<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>();
	Callable<Integer> task = null;
	for (int i = 0; i < 20; i++) {
		task = new Callable<Integer>() {
			@Override
			public Integer call() throws Exception {
				int ran = new Random().nextInt(1000);
				Thread.sleep(ran);
				System.out.println(Thread.currentThread().getName()
						+ " 休息了 " + ran);
				return ran;
			}
		};

		tasks.add(task);
	}

	long s = System.currentTimeMillis();
	
	List<Future<Integer>> results = exec.invokeAll(tasks);

	System.out.println("执行任务消耗了 :" + (System.currentTimeMillis() - s)
			+ "毫秒");

	for (int i = 0; i < results.size(); i++) {
		try {
			System.out.println(results.get(i).get());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	exec.shutdown();
}




  • 4
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
### 回答1: 我可以为您提供一个Gaussian批量提交任务脚本的示例:#!/bin/bash#Batch submission script for Gaussian#Specify the required memory and cpu time#PBS -l select=1:ncpus=12:mem=1gb#Specify the name of the job#PBS -N Gaussian_job#Submit the job to the queue#PBS -q gaussian_queue#Run the job from the current working directory#PBS -cwd#Write the output and error streams to separate files#PBS -e gaussian_job.err#PBS -o gaussian_job.out#Run the Gaussian calculations#g09 < input.com > output.log ### 回答2: Gaussian批量提交任务脚本是指在计算化学领域中,使用Gaussian软件进行计算时,编写的能够自动批量提交多个任务的脚本。 首先,需要先了解Gaussian软件的基本使用和批量任务提交的原理。Gaussian是一种常用的计算化学软件,可以用于研究分子结构、电子能级和反应动力学等化学问题。批量任务提交是指将多个不同的分子或化学计算模型输入到Gaussian中执行,并自动输出结果。 编写Gaussian批量提交任务脚本的过程大致可以分为以下几个步骤: 1. 创建输入文件列表:将所有需要计算的分子结构或化学计算模型保存为输入文件,并将这些文件的路径和名称记录在一个文本文件中。 2. 打开脚本工具:打开一个支持脚本编写的文本编辑器,例如Notepad++、Sublime Text等。 3. 编写循环语句:使用脚本语言编写循环语句,例如Python、Shell Script等,以便依次读取输入文件列表中的每个文件。 4. 提交任务:在循环语句中,调用Gaussian软件,使用指定的输入文件作为参数,提交任务到计算服务器。可以使用"subg09"或"g16"等Gaussian的执行命令。 5. 等待任务完成:由于计算任务通常需要一定时间才能完成,可以在脚本中增加等待语句,以便在任务完成后再进行下一步处理。 6. 处理结果文件:任务完成后,可以将输出文件进行处理和分析,提取感兴趣的结果数据。可以使用脚本语言中的文件操作和数据处理函数进行结果文件的读取和处理。 7. 循环下一个任务:完成当前任务的处理后,继续循环处理下一个任务,直到处理完所有的任务。 编写好的脚本可以根据需要进行多次运行,从而实现批量提交Gaussian计算任务的自动化操作,提高计算效率和减少重复工作。 ### 回答3: Gaussian 是一种流行的量子化学计算软件,批量提交任务是指同时提交多个 Gaussian 任务的脚本。在 Gaussian 中,任务可以通过两种方式进行提交:交互式提交批量提交批量提交任务脚本是为了简化任务提交的过程,提高任务的效率。下面是一个简单的 Gaussian 批量提交任务脚本示例: 1. 首先,创建一个文本文件,例如名为 `submit_script.txt`。 2. 在该文件中,按照以下格式编写 Gaussian 任务的相关信息: ``` %mem=10GB // 内存设置 %NProcShared=16 // 多核处理器设置 #P B3LYP/6-31G* // 计算方法和基组选择 任务1的输入信息 任务2的输入信息 任务3的输入信息 ... ``` 在每个任务的输入信息中,可以指定分子结构、计算类型、计算参数等。每个任务之间使用空行分隔。 3. 保存并关闭 `submit_script.txt` 文件。 4. 打开命令行界面,导航至该脚本所在的目录。 5. 输入以下命令来运行批量提交任务脚本: ``` g16 < submit_script.txt ``` 这将会依次执行每个任务,并将计算结果输出到相应的文件中。 通过批量提交任务脚本,我们可以一次性提交多个任务,而不需要手动逐个提交。这可以提高工作效率,特别是当需要进行大量 Gaussian 计算时。同时,还可以通过调整脚本中的计算参数,对多个任务进行统一的设置,保证计算的一致性。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

斗者_2013

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值