模拟并行任务分片

代码部分:

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. 【UVALive】2678 Subsequence(尺取法)

    题目 传送门:QWQ 分析 一开始没看到都是正整数根本不会做...... 看到了就是水题了.(但还是sb WA了一发) 尺取法搞一搞 代码 #include <bits/stdc++.h> ...

  2. 基于Keepalived高可用集群的MariaDB读写分离机制实现

    一 MariaDB读写分离机制 在实现读写分离机制之前先理解一下三种主从复制方式:1.异步复制:MariaDB默认的复制即是异步的,主库在执行完客户端提交的事务后会立即将结果返给给客户端,并不关心从库 ...

  3. [lua]判断nginx收到的是否json

    local post_data = ngx.req.get_body_data() --[[ngx.log(ngx.ERR, 'post data:', post_data)]] local ok, ...

  4. Python handling an exception

    #try...except... try: You do your operations here; ...................... except ExceptionI: If ther ...

  5. Fiddler监控面板显示Server栏(Fiddler v5.0)

    1.点击Rules下的Customize Rules.js,会打开Fiddler ScriptEditor 2.去掉 UI.lvSessions.AddBoundColumn("Server ...

  6. GridControl 添加全选列

    这里通过List对象绑定GridControl,且不用在GirdControl界面中添加任何列,实现CheckBox列的方法 1.列表中出现CheckBox列 非常简单,在绑定的List实体中,增加一 ...

  7. 关于python中的多进程模块multiprocessing

    python中的multiprocessing是一个多进程管理包,主要作用也就是提供多进程,而不是多线程,在其中用的比较多估计也就是Process和Pipe两个类,如下代码所示: #!/usr/bin ...

  8. leetcode744

    public class Solution { public char NextGreatestLetter(char[] letters, char target) { //a-97 z-122 v ...

  9. ffmpeg最简单的解码保存YUV数据 <转>

    video的raw data一般都是YUV420p的格式,简单的记录下这个格式的细节,如有不对希望大家能指出.   YUV图像通常有两种格式,一种是packet 还有一种是planar    从字面上 ...

  10. Spring 学习记录7 初识XmlWebApplicationContext

    主题 之前Spring相关的一些类,比如Enviromnent,BenFactory都接触了一些.有一些收获.但是直接看代码很多方法都不知为什么这样写.哪里会用到.因为太底层了.不好理解..现在从高层 ...