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();
}
}
}

最新文章

  1. broadcasting Theano vs. Numpy
  2. EL操作 web 对象的常用方法
  3. find_elements后点击不了抓取的元素
  4. how-to-install-siege-on-centos-7
  5. 聚合及UML表示
  6. ubuntu搭建java开发环境
  7. 使用Unity3D自带动画系统制作下雨效果
  8. yum安装lamp环境
  9. bootsraps ch1
  10. Linux下磁盘监控及系统版本-CPU-内存等查看
  11. canvas20181114
  12. 关于spring aop Advisor排序问题
  13. 【C++ Primer | 09】容器适配器
  14. C#模拟httpwebrequest请求_向服务器模拟cookie发送
  15. 将list集合转json
  16. python--linux上如何执行shell命令
  17. Java并发测试
  18. Haskell语言学习笔记(29)CPS
  19. 常用的SQL语句(牢记)
  20. Silverlight 之 新建项目解析

热门文章

  1. jsonHelper帮助类
  2. java--Proreties
  3. Jmeter吞吐量控制器
  4. 当MySQL数据库遇到Syn Flooding
  5. [20190507]sga_target=0注意修改_kghdsidx_count设置.txt
  6. 给普通用户赋予sudo权限后报错,提示/etc/sudoers文件权限拒绝
  7. Ansible配置执行远程主机的ssh端口号
  8. Sqlmap基础指令
  9. luoguP2882Face The Right Way
  10. python开发微信公众号SDK选择