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