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 一个复杂的例子的更多相关文章

  1. 用一个简单的例子来理解python高阶函数

    ============================ 用一个简单的例子来理解python高阶函数 ============================ 最近在用mailx发送邮件, 写法大致如 ...

  2. Spring-Context之一:一个简单的例子

    很久之前就想系统的学习和掌握Spring框架,但是拖了很久都没有行动.现在趁着在外出差杂事不多,就花时间来由浅入深的研究下Spring框架.Spring框架这几年来已经发展成为一个巨无霸产品.从最初的 ...

  3. 高仿“点触验证码”做的一个静态Html例子

    先上源码: <html> <head> <title>TouClick - Designed By MrChu</title> <meta htt ...

  4. 关于apriori算法的一个简单的例子

    apriori算法是关联规则挖掘中很基础也很经典的一个算法,我认为很多教程出现大堆的公式不是很适合一个初学者理解.因此,本文列举一个简单的例子来演示下apriori算法的整个步骤. 下面这个表格是代表 ...

  5. 一个UWSGI的例子

    摘要:uwsgi执行顺序:启动master进程,执行python脚本的公共代码(import同一层).然后生成worker进程,uwsgi.post_fork_hook=init_functions, ...

  6. 扩展Python模块系列(二)----一个简单的例子

    本节使用一个简单的例子引出Python C/C++ API的详细使用方法.针对的是CPython的解释器. 目标:创建一个Python内建模块test,提供一个功能函数distance, 计算空间中两 ...

  7. fitnesse - 一个简单的例子(slim)

    fitnesse - 一个简单的例子(slim) 2017-09-30 目录1 编写测试代码(Fixture code)2 编写wiki page并运行  2.1 新建wikiPage  2.2 运行 ...

  8. Struts2的配置和一个简单的例子

    Struts2的配置和一个简单的例子 笔记仓库:https://github.com/nnngu/LearningNotes 简介 这篇文章主要讲如何在 IntelliJ IDEA 中使用 Strut ...

  9. 一个简单的例子搞懂ES6之Promise

    ES5中实现异步的常见方式不外乎以下几种: 1. 回调函数 2. 事件驱动 2. 自定义事件(根本上原理同事件驱动相同) 而ES6中的Promise的出现就使得异步变得非常简单.promise中的异步 ...

随机推荐

  1. Laravel向表里插入字段

    执行: php artisan make:migration add_字段_to_表名_table --table=表名 此时在database/migrations文件夹下会生成一个相应文件,更改如 ...

  2. apicloud开发app

    1.apicloud官网 2.注册登录 3.开发控制台 4.创建应用 5.代码=>svn拉取代码,账号:注册账号的邮箱,密码:获取分支密码中的密码 6.编辑器下载对应的插件或者直接使用apicl ...

  3. maven 学习---Maven依赖管理

    其中一个Maven的核心特征是依赖管理.管理依赖关系变得困难的任务一旦我们处理多模块项目(包含数百个模块/子项目). Maven提供了一个高程度的控制来管理这样的场景. 传递依赖发现 这是很通常情况下 ...

  4. VUE-父组件和子组件

    1.父组件 const cnp2 = Vue.extend({ template: ` <div> <h2>我是构造器2</h2> <cpn1>< ...

  5. Promise的三兄弟:all(), race()以及allSettled()

    摘要: 玩转Promise. 原文:Promise 中的三兄弟 .all(), .race(), .allSettled() 译者:前端小智 Fundebug经授权转载,版权归原作者所有. 从ES6 ...

  6. Django 简单评论实现

    创建项目 django_comment 和应用 app01 修改 urls.py 文件 from django.contrib import admin from django.urls import ...

  7. odoo10学习笔记七:国际化、报表

    转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/11189297.html 一:国际化(模块翻译) 我们开发的模块要国际化通用,就要开发出不同的语言支持. 这些 ...

  8. PoI 3.17 已过时代码对比

    PoI 3.17 已过时代码对比颜色定义变化旧版本 : HSSFColor.BLACK.index新版本 : IndexedColors.BLACK.index 获取单元格格式旧版本 : cell.g ...

  9. 201871010107-公海瑜《面向对象程序设计(java)》第十一周学习总结

    201871010107-公海瑜<面向对象程序设计(java)>第十一周学习总结           项目                              内容   这个作业属于 ...

  10. 201871010125-王玉江《面向对象程序设计(java)》第十五周学习总结

    项目 内容 这个作业属于哪个课程 <任课教师博客主页链接> https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 <作业链接地址> ht ...