【T】并行调度
/**
* 并行调度相关处理
*
* 按卫星*日期 ,将待处理的任务分解为 卫星+日期 粒度的子任务 添加到paramMapList列表中
*/
List<Map<String, Object>> paramMapList = new ArrayList<Map<String, Object>>();
for (Iterator<OrderParamSatellite> iterator = paramSatellites.iterator(); iterator.hasNext();) {
OrderParamSatellite paramSatellite = iterator.next(); paramMapList.addAll(this.getParamMapList(paramSatellite));
} //根据集群最大处理能力,分页处理任务列表,作为list截取的步长 int fsize = HostServerQueue.getInstance().freeSize();
for(int i=0;i<paramMapList.size();i=i+fsize){
List<Map<String, Object>> tl = BXexample.getSubListPage(paramMapList, i, fsize);
//并行调度
BXexample.BXfunction(tl,new ExectueCallBack(){
public void doExectue(Object executor) throws Exception {
ExecuteOrderBTask((Map<String, Object>)executor);
}
}); //动态查找空闲节点数量,即集群最大处理能力
fsize = HostServerQueue.getInstance().freeSize();
}
package com.zlg.cobarclient; import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.dao.ConcurrencyFailureException; public class BXexample {
private static ExecutorService createCustomExecutorService(int poolSize, final String method) {
int coreSize = Runtime.getRuntime().availableProcessors();//返回系统CUP数量
if (poolSize < coreSize) {
coreSize = poolSize;
}
ThreadFactory tf = new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread t = new Thread(r, "thread created at BXexample method [" + method + "]");
t.setDaemon(true);
return t;
}
};
BlockingQueue<Runnable> queueToUse = new LinkedBlockingQueue<Runnable>();
final ThreadPoolExecutor executor = new ThreadPoolExecutor(coreSize, poolSize, 60,
TimeUnit.SECONDS, queueToUse, tf, new ThreadPoolExecutor.CallerRunsPolicy()); return executor;
} public static <T> List<T> getSubListPage(List<T> list, int skip,int pageSize) {
if (list == null || list.isEmpty()) {
return null;
}
int startIndex = skip;
int endIndex = skip + pageSize;
if (startIndex > endIndex || startIndex > list.size()) {
return null;
}
if (endIndex > list.size()) {
endIndex = list.size();
}
return list.subList(startIndex, endIndex);
} public static void BXfunction(Collection<?> paramCollection,final ExectueCallBack ecb){
//构建执行器
ExecutorService executor = createCustomExecutorService(Runtime.getRuntime().availableProcessors(), "batchExecuteProjection");
try {
//监视器
final CountDownLatch latch = new CountDownLatch(paramCollection.size());
final StringBuffer exceptionStaktrace = new StringBuffer();
Iterator<?> iter = paramCollection.iterator();
while (iter.hasNext()) {
final Object entity = iter.next();
Runnable task = new Runnable() {
public void run() {
try {
ecb.doExectue(entity);
} catch (Throwable t) {
exceptionStaktrace.append(ExceptionUtils.getFullStackTrace(t));
} finally {
latch.countDown();
}
}
};
executor.execute(task);//并行调度
} try {
latch.await();//监视器等待所有线程执行完毕
} catch (InterruptedException e) {
//调度异常
throw new ConcurrencyFailureException(
"unexpected interruption when re-arranging parameter collection into sub-collections ",e);
}
if (exceptionStaktrace.length() > 0) {
//业务异常
throw new ConcurrencyFailureException(
"unpected exception when re-arranging parameter collection, check previous log for details.\n"+ exceptionStaktrace);
} } finally {
executor.shutdown();//执行器关闭
}
}
}
package com.zlg.cobarclient;
public interface ExectueCallBack {
void doExectue(Object executor) throws Exception;
}
package com.zlg.cobarclient; import java.util.ArrayList;
import java.util.List; public class Hello {
public static void main(String[] args) {
List<String> paramCollection = new ArrayList<String>();
paramCollection.add("9");
paramCollection.add("2");
paramCollection.add("18");
paramCollection.add("7");
paramCollection.add("6");
paramCollection.add("1");
paramCollection.add("3");
paramCollection.add("4");
paramCollection.add("14");
paramCollection.add("13"); int freesize = 3;//当前处理能力 for(int i=0;i<paramCollection.size();i=i+freesize){ List<String> tl = BXexample.getSubListPage(paramCollection, i, freesize); BXexample.BXfunction(tl,new ExectueCallBack() {
public void doExectue(Object executor) throws Exception {
int k = Integer.parseInt((String)executor); for(int i=0;i<k*10000000;i++){
//执行循环
}
System.out.println(k+":hello world");
}
});
}
}
}
【T】并行调度的更多相关文章
- java并行调度框架封装及演示样例
參考资料: 阿里巴巴开源项目 CobarClient 源代码实现. 分享作者:闫建忠 分享时间:2014年5月7日 ---------------------------------------- ...
- .NET并行与多线程学习系列一
并行与多线程学习系列一 一.并行初试: public static void test() { ; i < ; i++) { Console.WriteLine(i); } } public s ...
- Elastic-Job 分布式调度平台
概述 referred:http://elasticjob.io/docs/elastic-job-lite/00-overview Elastic-Job是一个分布式调度解决方案,由两个相互独立的子 ...
- java定时调度器解决方案分类及特性介绍
什么是定时调度器? 我们知道程序的运行要么是由事件触发的,而这种事件的触发源头往往是用户通过ui交互操作层层传递过来的:但是我们知道还有另外一种由机器系统时间触发的程序运行场景.大家想想是否遇到或者听 ...
- quartz多任务调度+spring 实现
一.Quartz的学习简述 客官,不要急,请看完下面的内容... 代码可以直接拷贝使用,本文是编写2个定时方法来实现的,如果想要执行1个,删除另1个即可.但是想要知道执行原理请看最后的原理分析 二.执 ...
- 联童科技基于incubator-dolphinscheduler从0到1构建大数据调度平台之路
联童科技是一家智能化母婴童产业平台,从事母婴童行业以及互联网技术多年,拥有丰富的母婴门店运营和系统开发经验,在会员经营和商品经营方面,能够围绕会员需求,深入场景,更贴近合作伙伴和消费者,提供最优服务产 ...
- goroutine
Go语言从诞生到普及已经三年了,先行者大都是Web开发的背景,也有了一些普及型的书籍,可系统开发背景的人在学习这些书籍的时候,总有语焉不详的感觉,网上也有若干流传甚广的文章,可其中或多或少总有些与事实 ...
- 使用 Python 进行稳定可靠的文件操作
程序需要更新文件.虽然大部分程序员知道在执行I/O的时候会发生不可预期的事情,但是我经常看到一些异常幼稚的代码.在本文中,我想要分享一些如何在Python代码中改善I/O可靠性的见解. 考虑下述Pyt ...
- Quartz作业调度框架
Quartz 是一个开源的作业调度框架,它完全由 Java 写成,并设计用于 J2SE 和 J2EE 应用中.它提供了巨大的灵活性而不牺牲简单性.你能够用它来为执行一个作业而创建简单的或复杂的调度.本 ...
随机推荐
- Developing Backbone.js Applications
https://addyosmani.com/backbone-fundamentals/
- VC6.0 调试.dll文件
对于自己制作的.DLL文件,一直没有比较好的调试方法,其实是知道的太少. 下面就说说VC6.0下面 怎么调试DLL文件: 首先得有一个调用DLL文件的可执行程序,然后调用这个可执行程序. 在工程上 右 ...
- 蛙人(ple)
蛙人(ple) 题目描述 蛙人使用特殊设备潜水.设备中有一个气瓶,分两格:一格装氧气,另一格装氮气.留在水中有时间的限制,在深水中需要大量的氧气与氮气.为完成任务,蛙人必须安排好气瓶.每个气瓶可以用它 ...
- Rails (堆栈)<数据结构>
题意:<看图片> 解题思路:栈的简单应用: #include<iostream> #include<stack> #include<algorithm> ...
- DDS视图&Button控件
<Button android:id="@+id/btn1" android:layout_width="wrap_content" //包裹文字 ...
- 解决Only the original thread that created a view hierarchy can touch its views
这种异常出现在子线程中处理UI操作产生的异常,将UI操作放在主线程中就OK了
- css hack总结
虽然说,现在讨论css hanck已经过时了,不过了解下还是好的. 各游览器常用兼容标记一览表: 标记 IE6 IE7 IE8 FF Opera Sarari [*+><] √ √ X X ...
- adt download
http://my.oschina.net/roaminlove/blog/40384 http://dl.google.com/android/ADT-xx.x.x.zip 玩一下android的g ...
- ural1126 Magnetic Storms
Magnetic Storms Time limit: 0.5 secondMemory limit: 64 MB The directory of our kindergarten decided ...
- Windows上mxnet实战深度学习:Neural Net
前提: 假设已经在Windows上安装配置好mxnet和python语言包. 假设mxnet安装目录为D:\mxnet 假设已安装好wget 可以参考 这篇文章 打开Windows的命令提示符: 执行 ...