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() == 0) {
                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() == 0) {
                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 = 0; i < 10; i++) {
            list.add(new Table("table-" + event.id + "-" + i, i * 1000));
        }
        return list;
    }

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        Event[] events = {new Event(1), new Event(2)};
        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, 2, 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(10000));
            } 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(10000));
            } 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();
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/moris5013/p/11863664.html