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中的异步 ...
随机推荐
- 【转】聊一聊-JAVA 泛型中的通配符 T,E,K,V,?
原文:https://juejin.im/post/5d5789d26fb9a06ad0056bd9 前言 Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型 ...
- Gradle 创建java程序详细步骤
Java构建工具三强: Ant, Maven, GradleAnt历史悠久, 用build.xml 描述, 当时他的xml着实让很多工程师头痛, 但仍有用武之地. Maven 用pom.xml 文件描 ...
- vlan技术简单了解
VLAN(虚拟局域网)是对连接到的第二层交换机端口的网络用户的逻辑分段,不受网络用户的物理位置限制而根据用户需求进行网络分段.一个VLAN可以在一个交换机或者跨交换机实现.VLAN可以根据网络用户的位 ...
- liteos双向链表(十二)
1. 概述 1.1 基本概念 双向链表是指含有往前和往后两个方向的链表,即每个结点中除存放下一个节点指针外,还增加一个指向其前一个节点的指针.其头指针head是唯一确定的. 从双向链表中的任意一个结点 ...
- IEDA创建Springboot项目
随着技术的更新对于开发速度的追求,我们越来越不能忍受的是Spring框架对于集成开发以后大量的配置问题.所以SprigBoot应运而生,SpringBoot框架其实就是在Spring框架的外边包裹上了 ...
- FusionSphere产品功能
华为Fusion产品 FusionSphere解决方案的三种场景 服务器虚拟化 NFVI:运营商基于openstack kvm协议的解决方案 云数据中心 FusionSphere服务器虚拟化解决方案 ...
- 15、iptables详解
-- http://www.netfilter.org/ http://www.iptables.org/ --参考路径 http://www.netfilter.org/docume ...
- 201871010101-陈来弟《面向对象程序设计(Java)》第八周学习总结
实验七 接口的定义与使用 第一部分:理论知识 一.接口.lambda和内部类: Comparator与comparable接口: 1.comparable接口的方法是compareTo,只有一个参数 ...
- 201871010105-曹玉中《面向对象程序设计(java)》第十二周学习总结
201871010105-曹玉中<面向对象程序设计(java)>第十二周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...
- 201871010133-赵永军《面向对象程序设计(java)》第十三周学习总结
201871010133-赵永军<面向对象程序设计(java)>第十三周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...