All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lyncode.jtwig.render.stream.RenderControl Maven / Gradle / Ivy

package com.lyncode.jtwig.render.stream;

import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by rsilva on 3/23/14.
 */
public class RenderControl {

    private AtomicInteger mStackCounter;
    private Semaphore mSemaphore;

    public RenderControl() {
        mStackCounter = new AtomicInteger(0);
        mSemaphore = new Semaphore(0);
    }

    public void waitFinish() throws InterruptedException {
        while (mStackCounter.get() > 0) {
            mSemaphore.acquire();
        }
    }

    public void push() {
        mStackCounter.incrementAndGet();
    }

    public void cancel() {
        int value = mStackCounter.getAndSet(0);
        if (value <= 0) {
            mSemaphore.release();
        }
    }

    public void poll() {
        int value = mStackCounter.decrementAndGet();
        if (value <= 0) {
            mSemaphore.release();
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy