/**
* 并行调度相关处理
*
* 按卫星*日期 ,将待处理的任务分解为 卫星+日期 粒度的子任务 添加到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】并行调度的更多相关文章

  1. java并行调度框架封装及演示样例

    參考资料:  阿里巴巴开源项目 CobarClient  源代码实现. 分享作者:闫建忠 分享时间:2014年5月7日 ---------------------------------------- ...

  2. .NET并行与多线程学习系列一

    并行与多线程学习系列一 一.并行初试: public static void test() { ; i < ; i++) { Console.WriteLine(i); } } public s ...

  3. Elastic-Job 分布式调度平台

    概述 referred:http://elasticjob.io/docs/elastic-job-lite/00-overview Elastic-Job是一个分布式调度解决方案,由两个相互独立的子 ...

  4. java定时调度器解决方案分类及特性介绍

    什么是定时调度器? 我们知道程序的运行要么是由事件触发的,而这种事件的触发源头往往是用户通过ui交互操作层层传递过来的:但是我们知道还有另外一种由机器系统时间触发的程序运行场景.大家想想是否遇到或者听 ...

  5. quartz多任务调度+spring 实现

    一.Quartz的学习简述 客官,不要急,请看完下面的内容... 代码可以直接拷贝使用,本文是编写2个定时方法来实现的,如果想要执行1个,删除另1个即可.但是想要知道执行原理请看最后的原理分析 二.执 ...

  6. 联童科技基于incubator-dolphinscheduler从0到1构建大数据调度平台之路

    联童科技是一家智能化母婴童产业平台,从事母婴童行业以及互联网技术多年,拥有丰富的母婴门店运营和系统开发经验,在会员经营和商品经营方面,能够围绕会员需求,深入场景,更贴近合作伙伴和消费者,提供最优服务产 ...

  7. goroutine

    Go语言从诞生到普及已经三年了,先行者大都是Web开发的背景,也有了一些普及型的书籍,可系统开发背景的人在学习这些书籍的时候,总有语焉不详的感觉,网上也有若干流传甚广的文章,可其中或多或少总有些与事实 ...

  8. 使用 Python 进行稳定可靠的文件操作

    程序需要更新文件.虽然大部分程序员知道在执行I/O的时候会发生不可预期的事情,但是我经常看到一些异常幼稚的代码.在本文中,我想要分享一些如何在Python代码中改善I/O可靠性的见解. 考虑下述Pyt ...

  9. Quartz作业调度框架

    Quartz 是一个开源的作业调度框架,它完全由 Java 写成,并设计用于 J2SE 和 J2EE 应用中.它提供了巨大的灵活性而不牺牲简单性.你能够用它来为执行一个作业而创建简单的或复杂的调度.本 ...

随机推荐

  1. 怎样让外界无法改变自定义view的尺寸大小

    重写setFrame和setBounds方法即可. + (instancetype)testView { return [[self alloc] init]; } - (void)setFrame: ...

  2. 阿里云CentOS安装firefox闪退

    安装完vnc远程连接,接着安装firefox,点firefox木有任何提示就退 直接命令下运行,发现错误如下: /usr/lib/firefox/firefox: symbol lookup erro ...

  3. CentOS 6.5搭建Samba服务器

    目标需求:在Windows7下访问CentOS 6.5 root用户桌面/ZS文件夹 0.准备工作 关闭防火墙并开启不起动 service iptables stop chkconfig iptabl ...

  4. The Triangle 经典DP

    题意:数塔问题 思路:1:递归.2:递推.3:记忆化搜索.<刘汝佳,第九章> #include<iostream> #include<cstdio> #includ ...

  5. hudson--ant编写记录

    最近配置Hudson---持续集成工具,重点是ant的编写. 环境:Ubuntu 虚拟机 hudson系统设置里面jdk ant路径也是Ubuntu里文件路径如:/home/test/java/ant ...

  6. C#获取IPv4代码

    using System; using System.Collections.Generic; using System.Collections.Specialized; using System.C ...

  7. Android - Get Bluetooth UUID for this device

    Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join th ...

  8. Unexpected exception 'Cannot run program ... error=2, No such file or directory' ... adb'

    Eclipse ADT Unexpected exception 'Cannot run program' up vote 8 down vote favorite 4 I have installe ...

  9. 数据结构录 之 单调队列&单调栈。

    队列和栈是很常见的应用,大部分算法中都能见到他们的影子. 而单纯的队列和栈经常不能满足需求,所以需要一些很神奇的队列和栈的扩展. 其中最出名的应该是优先队列吧我觉得,然后还有两种比较小众的扩展就是单调 ...

  10. 如何在Android中添加系统服务

    一,在frameworks/base/core/java/android/content/Context.java中添加 public static final String RADIO_SERVIC ...