批量执行,同步实现

protected static class SyncTaskHelper {
        private volatile int count = 0;

        public void enter() {
            synchronized (this) {
                this.count++;
            }
        }

        public int get() {
            return this.count;
        }

        public int leave() {
            synchronized (this) {
                this.count--;
                if (this.count == 0) {
                    this.notifyAll();
                }
                return this.count;
            }
        }
    }
public void flush(boolean sync) {
        SyncTaskHelper syncHelper = null;
        if (sync) {
            syncHelper = new SyncTaskHelper();
        }
        this.flushRemove(syncHelper);
        this.flushUpdate(syncHelper);
        if (null != syncHelper) {
            synchronized (syncHelper) {
                while (syncHelper.get() != 0) {
                    try {
                        syncHelper.wait();
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                }
            }
        }
    }

  

  

原文地址:https://www.cnblogs.com/alipayhutu/p/2934920.html