模拟并行任务分片

代码部分:

package com.pool;

import com.alibaba.fastjson.JSON;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger; /**
* Created on 2016/5/4.
*/
public class ScheduledPoolTest { public static void main(String[] args) {
int len = 3; // 定时任务线程池,长度为3
ScheduledThreadPoolExecutor executor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(len); // 添加3个任务,每5秒执行一次,并行处理
for (int i = 0; i < 3; i++) {
executor.scheduleAtFixedRate(new TaskThread(len, i), 0L, 5L, TimeUnit.SECONDS);
} /**
* 手动关闭pool,没什么太大的用
*/
try (
Reader reader = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(reader)
) {
while (true) {
String line = in.readLine();
if (line.equalsIgnoreCase("no")) {
executor.shutdown();
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} } } /**
* 模拟切片任务
*/
class TaskThread implements Runnable { private int increase = 0;
private List<Integer> list; // 切片总数
private int len;
// 切片余数
private int mod;
private int size = 10000; public TaskThread(int len, int mod) {
this.len = len;
this.mod = mod;
} @Override
public void run() {
list = new ArrayList<>();
for (int i = increase * size; i < (increase + 1) * size; i++) {
// 根据 mod 分片
if (i % len == mod) {
list.add(i);
}
}
increase++;
System.out.println("**********************************************************");
System.out.println(Thread.currentThread().getName() + " : " + JSON.toJSONString(list));
System.out.println("**********************************************************");
} }

运行效果:

**********************************************************
**********************************************************
**********************************************************
pool-1-thread-2 : [1,4,7,10,13,16,19,22,25,28,31,34,37,40,43,46,49,52,55,58,61,64,67,70,73,76,79,82,85,88,91,94,97]
**********************************************************
pool-1-thread-3 : [2,5,8,11,14,17,20,23,26,29,32,35,38,41,44,47,50,53,56,59,62,65,68,71,74,77,80,83,86,89,92,95,98]
**********************************************************
pool-1-thread-1 : [0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99]
**********************************************************
**********************************************************
pool-1-thread-1 : [102,105,108,111,114,117,120,123,126,129,132,135,138,141,144,147,150,153,156,159,162,165,168,171,174,177,180,183,186,189,192,195,198]
**********************************************************
**********************************************************
pool-1-thread-1 : [100,103,106,109,112,115,118,121,124,127,130,133,136,139,142,145,148,151,154,157,160,163,166,169,172,175,178,181,184,187,190,193,196,199]
**********************************************************
**********************************************************
pool-1-thread-1 : [101,104,107,110,113,116,119,122,125,128,131,134,137,140,143,146,149,152,155,158,161,164,167,170,173,176,179,182,185,188,191,194,197]
**********************************************************
no
close the pool Process finished with exit code 0

只是模拟下在线程池中分片,用的是定时pool

1. 所有属性都是对象的,避免共享
2. 使用分片,确保数据不被多次处理(多个线程处理)
3. 使用多线程处理,提高效率

今天没看其它东东,昨天刚好想到这个,就写了个小demo,只是测试使用,ScheduledThreadPoolExecutor用的不多,一般定时机制都会选择quartz,更灵活与强大!

schedule-pool模拟并行任务分片的更多相关文章

  1. 在一台机器上模拟mongodb分片

    首先选择一个目录在其中建立以下2个文件夹:data和log 在data下建立9个文件夹: 其中前3个为配置服务器所在文件夹,按照官网要求,一个集群需要3个config server rs-a-n和rs ...

  2. GNS3 模拟icmp分片不可达

    R1 : conf t int f0/0 no shutdown ip add 192.168.1.1 255.255.255.0 no ip routing end R2 f0/0: conf t ...

  3. win7环境下mongodb分片和移除

    本文主要介绍在一台win7电脑上模拟mongo分片.如果有多台服务器,可以将每个mongo部署在单台电脑上.我们将配置3个mongo分片,3个配置服务器,1个路由服务器.如下图所示进行配置,介绍如何增 ...

  4. LoadRunner压力测试实例

    1 LoadRunner 概要介绍... 2 .项目背景介绍... 5 .使用LoadRunner进行负载/ 实施测试... 16 6.1 Memory相关... 22 6.2 Processor相关 ...

  5. 《实战Java高并发程序设计》读书笔记

    文章目录 第二章 Java并行程序基础 2.1 线程的基本操作 2.1.1 线程中断 2.1.2 等待(wait)和通知(notify) 2.1.3 等待线程结束(join)和谦让(yield) 2. ...

  6. Function.prototype.toString 的使用技巧

    Function.prototype.toString这个原型方法可以帮助你获得函数的源代码, 比如: function hello ( msg ){ console.log("hello& ...

  7. 转:loadrunner经典面试题

    在LoadRunner中为什么要设置思考时间和pacing 答: 录制时记录的是客户端和服务端的交互,如果要精确模拟 用户的行为,那么客户操作客户端时花费了很多时间要怎么模拟呢?录入 填写提交的内容, ...

  8. LoadRunner面试题

    在LoadRunner中为什么要设置思考时间和pacing 答: 录制时记录的是客户端和服务端的交互,如果要精确模拟 用户的行为,那么客户操作客户端时花费了很多时间要怎么模拟呢?录入 填写提交的内容, ...

  9. 分布式任务调度平台XXL-JOB

    <分布式任务调度平台XXL-JOB>       一.简介 1.1 概述 XXL-JOB是一个轻量级分布式任务调度框架,其核心设计目标是开发迅速.学习简单.轻量级.易扩展.现已开放源代码并 ...

随机推荐

  1. Windows下编译sqlite3

    一.下载 sqlite-amalgamation-3240000:sqlite源代码,主要需要头文件sqlite3.h sqlite-dll-win32-x86-3240000.zip:sqlite3 ...

  2. Python Twisted系列教程22:结束

    作者:dave@http://krondo.com/part-22-the-end/  译者: Cheng Luo 你可以从”第一部分 Twist理论基础“开始阅读:也可以从”Twisted 入门!“ ...

  3. [Z] 关于Python Tornado的一些资料

    一个简单的样例: http://osedu.net/article/python/2014-03-18/501.html ioloop的官方doc: http://www.tornadoweb.org ...

  4. 爬虫的三种解析方式(正则解析, xpath解析, bs4解析)

    一 : 正则解析 : 常用正则回顾: 单字符: . : 除换行符以外的所有字符 [] : [aoe] [a-w] 匹配集合中任意一个字符 \d : 数字 [0-9] \D : 非数字 \w : 非数字 ...

  5. leetcode8

    public class Solution { public int MyAtoi(string str) { , sign = , total = ; //1. Empty string ) { ; ...

  6. struts.xml配置文件(package,namespace,action)

    struts2.0 xml配置 struts.xml文件结构 struts.xml文件是整个Struts2框架的核心. struts.xml文件内定义了Struts2的系列Action,定义Actio ...

  7. LInux命令执行http请求

    Linux下提供了一些命令可以直接执行http请求,下面举例来介绍几个命令. http // GET请求 http http://127.0.0.1:2379/version http GET htt ...

  8. pip / conda 导出和安装环境组件 requirements.txt

    pip 批量导出包含环境中所有组件的requirements.txt文件 pip freeze > requirements.txt pip 批量安装requirements.txt文件中包含的 ...

  9. Elasticsearch学习系列之term和match查询实例

    Elasticsearch查询模式 一种是像传递URL参数一样去传递查询语句,被称为简单查询 GET /library/books/_search //查询index为library,type为boo ...

  10. 前端开发之CSS篇三

    主要内容:  一.CSS布局之浮动     二.清除浮动带来的问题     三.margin塌陷问题和水平居中     四.善用父级的的padding取代子级的margin     五.文本属性和字体 ...