Java 8 Semaphore - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 Semaphore class.

This code example shows how to use the following methods:acquire, release
     * 
     * @return 
     * @throws Exception 
     */ 
    public MessageOutputStream acquireChannelMessageOutputStream() throws Exception { 
        this.channelWriteSemaphore.acquire();
        try { 
            return this.channel.writeMessage();
        } catch (Exception e) {
            // release 
            this.channelWriteSemaphore.release();
            throw e;
        } 
    } 
 
    /** 
     * Releases a previously held permit/lock on a message outputstream of a channel and also closes 
     * the <code>messageOutputStream</code> 
     * 
     * @param messageOutputStream The message outputstream 
8
CodeRank
This code example shows how to use the following methods:acquire, getQueueLength, release
 
    @Override 
    public SearchResults search(Query query, int start, int limit, int scoringFunctionIndex, Map<String, String> extraParameters) throws InterruptedException {
        // call delegate searcher 
        try { 
            int queueLen = semaphore.getQueueLength();
            if (queueLen >= maxSearchQueueLength) {
                logger.warn("Too many waiting to search, queue length = " + queueLen + ", returning without searching");
                throw new InterruptedException("Too many concurrent searches");
            } 
            if (queueLen > 0) {
                logger.warn("Concurrent searches queue length is " + queueLen + ", will wait");
            } 
            // consider adding a timeout to this call to semaphore.acquire() 
            semaphore.acquire();
            try { 
                return this.delegate.search(query, start, limit, scoringFunctionIndex, extraParameters);
            } finally { 
                semaphore.release();
            } 

7
CodeRank
This code example shows how to use the following methods:drainPermits, release
 
    private void setBudget(int numOfDocs) {
        logger.debug("updating budget to [{}]", numOfDocs);
        if (numOfDocs >= 0) {
            hasBudget.set(true);
            availableBudget.release(numOfDocs);
        } else { 
            hasBudget.set(false);
        } 
 
    } 
 
    /** Start indexing with no limit to the number of documents */ 
    public void start() { 
        start(-1); 
    } 
 
    /** 
     * Start indexing 
     * 

7
CodeRank
This code example shows how to use the following methods:acquire
 
    private boolean permitAcquired(final long waitTime) throws InterruptedException {
        if (waitTime > 0) {
            return _sem.tryAcquire(waitTime, TimeUnit.MILLISECONDS);
        } else if (waitTime < 0) {
            _sem.acquire();
            return true; 
        } else { 
            return _sem.tryAcquire();
        } 
    } 
 
    /** Clears the pool of all objects. */ 
    protected synchronized void close(){ 
        _closed = true;
        for (T t : _avail)
            cleanup(t);
        _avail.clear();
        _out.clear();
    } 
7
CodeRank
This code example shows how to use the following methods:acquire, release
     */ 
    public <T> T request(OpPrompt<T> opPrompt) {
        Object response = null; 
        Log.d(TAG, "request for " + opPrompt);
        try { 
            promptToken.acquire();
            this.opPrompt = opPrompt;
 
            promptUIRegistry.get().displayPrompt(); 
 
            // acquire lock until user passes back value 
            promptResponse.acquire();
 
            response = popResponse(); 
        } catch (InterruptedException e) {
            Log.e(TAG, "InterruptedException while waiting for " + opPrompt, e);
        } finally { 
            promptToken.release();
        } 
 

7
CodeRank
This code example shows how to use the following methods:tryAcquire
 
        super.tearDown(); 
    } 
 
    private void runOnLooper(final Runnable command) throws InterruptedException {
        final Semaphore sem = new Semaphore(0);
        mHandler.post(new Runnable() {
            @Override 
            public void run() { 
                try { 
                    command.run();
                } finally { 
                    sem.release();
                } 
            } 
        }); 
        if (! sem.tryAcquire(WAIT_TIMEOUT, TimeUnit.MILLISECONDS)) {
            fail("Failed to run the command on the looper.");
        } 
    } 

7
CodeRank
This code example shows how to use the following methods:release
        if (ROOT_LOGGER.isTraceEnabled()) { 
            ROOT_LOGGER.tracef("Discard instance %s#%s", this, ctx);
        } 
 
        // If we block when maxSize instances are in use, invoke release on strictMaxSize 
        semaphore.release();
 
        // Let the super do any other remove stuff 
        super.doRemove(ctx);
    } 
 
    public int getCurrentSize() { 
        return getCreateCount() - getRemoveCount(); 
    } 
 
    public int getAvailableCount() { 
        return semaphore.availablePermits();
    } 
 
    public int getMaxSize() { 
7
CodeRank
This code example shows how to use the following methods:acquire
            throw new IllegalStateException("Do not call this method from the UI thread!");
        } 
 
        mForceSize = new Size(width, height);
 
        final Semaphore waiter = new Semaphore(0);
 
        // Layout with new size 
        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override 
            public void onGlobalLayout() { 
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                    getViewTreeObserver().removeGlobalOnLayoutListener(this);
                } else { 
                    getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } 
                waiter.release();
            } 
        }); 
        post(new Runnable() {

7
CodeRank
This code example shows how to use the following methods:drainPermits
  public Collection<V> getResources() {
    return unmodifiableresources;
  } 
 
  public void close() { 
    sem.drainPermits();
  } 
   
  public void remove(final V res) {
  	this.resources.remove(res);
  } 
} 
6
CodeRank
This code example shows how to use the following methods:acquire, release
     * to retrieve the response or result() to retrieve the just the results.</p> 
     * 
     * @throws InterruptedException on interruption. 
     */ 
    public void waitForResponse() throws InterruptedException {
        m_lock.acquire();
        m_lock.release();
    } 
 
    /** 
     * <p>Return the arguments provided with the procedure invocation.</p> 
     * @return Object array containing procedure arguments 
     */ 
    @Override 
    public Object[] args() { 
        return super.args(); 
    } 
} 
7
CodeRank

Related Java 8 Semaphore Questions & Answers:

See Code Examples for Java 8 Semaphore Methods: