CountDownLatch 一个复杂的例子
CountDownLatch复杂点的例子
public class CountDownLatchTest2 {
private static Random random = new Random(System.currentTimeMillis());
static class Event {
int id;
Event(int id) {
this.id = id;
}
}
static class TaskBatch {
private CountDownLatch latch;
private EventBatch eventBatch;
private Table table;
TaskBatch(Table table, int size, EventBatch eventBatch) {
latch = new CountDownLatch(size);
this.eventBatch = eventBatch;
this.table = table;
}
public void done() {
latch.countDown();
if (latch.getCount() == ) {
System.out.println("the table " + table.name + " has finished [ " + table + " ]");
eventBatch.done();
}
}
}
static class EventBatch {
private CountDownLatch latch;
private Event event;
EventBatch(int size, Event event) {
this.latch = new CountDownLatch(size);
this.event = event;
}
public void done() {
latch.countDown();
if (latch.getCount() == ) {
System.out.println(" ====== all of tables done in event " + event.id);
}
}
}
static class Table {
String name;
long sourceRecordCount;
long targetRecordCount;
String sourceColumnSchema;
String targetColumnSchema;
Table(String name, long sourceRecordCount) {
this.name = name;
this.sourceRecordCount = sourceRecordCount;
}
@Override
public String toString() {
return "Table{" +
"name='" + name + '\'' +
", sourceRecordCount=" + sourceRecordCount +
", targetRecordCount=" + targetRecordCount +
", sourceColumnSchema='" + sourceColumnSchema + '\'' +
", targetColumnSchema='" + targetColumnSchema + '\'' +
'}';
}
}
private static List<Table> capture(Event event) {
List<Table> list = new ArrayList<Table>();
for (int i = ; i < ; i++) {
list.add(new Table("table-" + event.id + "-" + i, i * ));
}
return list;
}
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool();
Event[] events = {new Event(), new Event()};
for (Event event : events) {
List<Table> tables = capture(event);
EventBatch eventBatch = new EventBatch(tables.size(), event);
for (Table table : tables) {
TaskBatch taskBatch = new TaskBatch(table, , eventBatch);
TrustSourceRecordCount trustSourceRecordCount = new TrustSourceRecordCount(table, taskBatch);
TrustSourceColumnSchema trustSourceColumnSchema = new TrustSourceColumnSchema(table, taskBatch);
executorService.submit(trustSourceRecordCount);
executorService.submit(trustSourceColumnSchema);
}
}
}
static class TrustSourceRecordCount implements Runnable {
private Table table;
private TaskBatch taskBatch;
TrustSourceRecordCount(Table table, TaskBatch taskBatch) {
this.table = table;
this.taskBatch = taskBatch;
}
@Override
public void run() {
try {
Thread.sleep(random.nextInt());
} catch (InterruptedException e) {
e.printStackTrace();
}
table.targetRecordCount = table.sourceRecordCount;
// System.out.println("table "+table.name + " target recordCount have done and update the data");
taskBatch.done();
}
}
static class TrustSourceColumnSchema implements Runnable {
private Table table;
private TaskBatch taskBatch;
TrustSourceColumnSchema(Table table, TaskBatch taskBatch) {
this.table = table;
this.taskBatch = taskBatch;
}
@Override
public void run() {
try {
Thread.sleep(random.nextInt());
} catch (InterruptedException e) {
e.printStackTrace();
}
table.targetColumnSchema = table.sourceColumnSchema;
// System.out.println("table "+table.name + " target columnSchema have done and update the data");
taskBatch.done();
}
}
}
CountDownLatch 一个复杂的例子的更多相关文章
- 用一个简单的例子来理解python高阶函数
============================ 用一个简单的例子来理解python高阶函数 ============================ 最近在用mailx发送邮件, 写法大致如 ...
- Spring-Context之一:一个简单的例子
很久之前就想系统的学习和掌握Spring框架,但是拖了很久都没有行动.现在趁着在外出差杂事不多,就花时间来由浅入深的研究下Spring框架.Spring框架这几年来已经发展成为一个巨无霸产品.从最初的 ...
- 高仿“点触验证码”做的一个静态Html例子
先上源码: <html> <head> <title>TouClick - Designed By MrChu</title> <meta htt ...
- 关于apriori算法的一个简单的例子
apriori算法是关联规则挖掘中很基础也很经典的一个算法,我认为很多教程出现大堆的公式不是很适合一个初学者理解.因此,本文列举一个简单的例子来演示下apriori算法的整个步骤. 下面这个表格是代表 ...
- 一个UWSGI的例子
摘要:uwsgi执行顺序:启动master进程,执行python脚本的公共代码(import同一层).然后生成worker进程,uwsgi.post_fork_hook=init_functions, ...
- 扩展Python模块系列(二)----一个简单的例子
本节使用一个简单的例子引出Python C/C++ API的详细使用方法.针对的是CPython的解释器. 目标:创建一个Python内建模块test,提供一个功能函数distance, 计算空间中两 ...
- fitnesse - 一个简单的例子(slim)
fitnesse - 一个简单的例子(slim) 2017-09-30 目录1 编写测试代码(Fixture code)2 编写wiki page并运行 2.1 新建wikiPage 2.2 运行 ...
- Struts2的配置和一个简单的例子
Struts2的配置和一个简单的例子 笔记仓库:https://github.com/nnngu/LearningNotes 简介 这篇文章主要讲如何在 IntelliJ IDEA 中使用 Strut ...
- 一个简单的例子搞懂ES6之Promise
ES5中实现异步的常见方式不外乎以下几种: 1. 回调函数 2. 事件驱动 2. 自定义事件(根本上原理同事件驱动相同) 而ES6中的Promise的出现就使得异步变得非常简单.promise中的异步 ...
随机推荐
- synchronized和volatile使用
synchronized和volatile volatile :保证内存可见性,但是不保证原子性: synchronized:同步锁,既能保证内存可见性,又能保证原子性: synchronized实现 ...
- deepin添加设置快捷键
deepin的设置侧边栏没有快捷键需要手动设置,第一步就是要知道设置的命令是什么. 按下start,把其中的"控制中心"发送到桌面,以文本方式打开之,其中的exec字段就是打开设置 ...
- SAP MM 同一个序列号可以被多次用在交货单发货过账?
SAP MM 同一个序列号可以被多次用在交货单发货过账? 如下公司间转储订单,从公司代码CSAS转入公司代码HKCS, 物料有启用序列号管理. 转储数量为5 PC.该STO单据共计有2个外向交货单 8 ...
- vue学习指南:第一篇 - vue的介绍
三大主流框架: 1. Vue.js 是目前最火的一个前端框架,react是最流行的前端框架 (react除了开发网站,还可以开发手机app,Vue语法也是可以用于手机App开发的,需要借助于wexx) ...
- SQL的简单函数(极客时间)
SQL的函数 函数就是将我们经常使用的代码封装起来, 需要的时候直接调用, 提高代码效率和可维护性. SQL函数一般是在数据上执行, 可以方便地转换和处理数据. 常用的SQL函数 SQL提供了一些常用 ...
- JMETER 审批任务实战
业务场景 我们需要对流程任务进行审批,这个和流程发起是不一样的,因为在流程发起时,只需要用户登录后,指定固定的流程方案和数据就可以发起流程了. 流程任务是需要获取任务ID再做任务审批的. 实现思路 1 ...
- [PHP] 项目实践中的自动加载实现
1.使用spl_autoload_register函数注册一个自己定义的自动加载函数2.当在代码中new一个不存在的类的时候,上面的函数会被调用,不存在的类名会被作为参数传入该函数中3.兼容了两种方式 ...
- Linux7/Centos7用户密码安全
当Linux7/Centos7的用户root密码常用如下方式找回密码: 第一步:按e键进入内核启动页面如下: 第二步:在linux16一行LANG=zh_CN.UTF-8后面加rd.break con ...
- 爬虫---Beautiful Soup 通过添加不同的IP请求
上一篇爬虫写了如何应付反爬的一些策略也简单的举了根据UA的例子,今天写一篇如何根据不同IP进行访问豆瓣网获取排行版 requests添加IP代理 如果使用代理的话可以通过requests中的方法pro ...
- 学习9:MongoDB知识
MongoDB学习笔记 1 基本介绍 基本概念 MongoDB**是一种面向文档的数据库管理系统,由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.2007年10月,MongoDB由10g ...