批量执行,同步实现

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

  

  

 

 

posted @ 2013-02-27 12:01  技术草根女  Views(245)  Comments(0Edit  收藏  举报