Refine search
@Override public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { if (unit == null) { throw new NullPointerException("unit"); } if (inEventLoop()) { throw new IllegalStateException("cannot await termination of the current thread"); } if (threadLock.tryAcquire(timeout, unit)) { threadLock.release(); } return isTerminated(); }
@Override public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { if (readerSemaphore.tryAcquire(time, unit)) { int oldNumReaders = numReaders; numReaders++; if (numReaders == 1) { if (writerSemaphore.tryAcquire(time, unit)) { readerSemaphore.release(); return true; } else { numReaders = oldNumReaders; readerSemaphore.release(); return false; } } else { readerSemaphore.release(); return true; } } return false; }
@Override public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { if (unit == null) { throw new NullPointerException("unit"); } if (inEventLoop()) { throw new IllegalStateException("cannot await termination of the current thread"); } if (threadLock.tryAcquire(timeout, unit)) { threadLock.release(); } return isTerminated(); }
/** {@inheritDoc} */ @Override public void leave(IClient client, IScope scope) { try { lock.tryAcquire(1, TimeUnit.SECONDS); super.leave(client, scope); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.release(); } }
/** {@inheritDoc} */ @Override public void stop(IScope scope) { try { lock.tryAcquire(1, TimeUnit.SECONDS); super.stop(scope); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.release(); } }
/** {@inheritDoc} */ @Override public void disconnect(IConnection conn, IScope scope) { try { lock.tryAcquire(1, TimeUnit.SECONDS); super.disconnect(conn, scope); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.release(); } }
if (!guard.tryAcquire()) { return; logger.error("dump jStack error", t); } finally { guard.release();
if (!guard.tryAcquire()) { return; logger.error("dump jStack error", t); } finally { guard.release();
/** {@inheritDoc} */ @Override public boolean connect(IConnection conn, IScope scope, Object[] params) { try { lock.tryAcquire(1, TimeUnit.SECONDS); return super.connect(conn, scope, params); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.release(); } return false; }
/** {@inheritDoc} */ @Override public boolean join(IClient client, IScope scope) { try { lock.tryAcquire(1, TimeUnit.SECONDS); return super.join(client, scope); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.release(); } return false; }
/** {@inheritDoc} */ @Override public boolean start(IScope scope) { if (lock == null) { lock = new Semaphore(1, true); } try { lock.tryAcquire(1, TimeUnit.SECONDS); return super.start(scope); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.release(); } return false; }
@Override public void startIt() { if (semaphore.tryAcquire()) { try { executorService.execute(this::doDatabaseMigration); } catch (RuntimeException e) { semaphore.release(); throw e; } } else { LOGGER.trace("{}: lock is already taken or process is already running", Thread.currentThread().getName()); } }
@Override public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { if (unit == null) { throw new NullPointerException("unit"); } if (inEventLoop()) { throw new IllegalStateException("cannot await termination of the current thread"); } if (threadLock.tryAcquire(timeout, unit)) { threadLock.release(); } return isTerminated(); }
/** * Wait for any pending operations, and notify the the completions that the operations and done. */ @Override public void waitForOperations() { boolean interrupted = false; while (!executor.isShutdown()) { try { if (operationSemaphore.tryAcquire(maxParallelOperations, 1, TimeUnit.SECONDS)) { operationSemaphore.release(maxParallelOperations); drainCompletions(); if (interrupted) { Thread.currentThread().interrupt(); } return; } } catch (InterruptedException e) { interrupted = true; } } }
public static void main(String[] args) { ExecutorService executor = Executors.newCachedThreadPool(); Semaphore semaphore = new Semaphore(3); Runnable r = () -> { try { System.out.println("Trying to acquire - " + Thread.currentThread().getName()); if (semaphore.tryAcquire(2, TimeUnit.SECONDS)) { // use-get resource // simulate work in progress System.out.println("Acquired - " + Thread.currentThread().getName()); Thread.sleep(2000); System.out.println("Done - " + Thread.currentThread().getName()); } } catch (InterruptedException e) { e.printStackTrace(); } finally { semaphore.release(); } }; for (int i = 0; i < 4; i++) { executor.execute(r); } executor.shutdown(); } }
@Override public void run() { try { liveThreads.add(Thread.currentThread()); try { queue.remove().run(); } finally { liveThreads.remove(Thread.currentThread()); } } finally { if (queue.isEmpty()) { runnerPermit.release(); if (!queue.isEmpty() && runnerPermit.tryAcquire()) { executor.submit(this); } else if (isTerminated()) { termination.countDown(); } } else { executor.submit(this); } } } });
public void setMaxThreads(final int maxThreads) { Assert.checkMinimumParameter("maxThreads", 0, maxThreads); synchronized (lock) { final int old = this.maxThreads; final int diff = old - maxThreads; if (diff < 0) { limitSemaphore.release(-diff); } else if (diff > 0) { if (! limitSemaphore.tryAcquire(diff)) { throw Messages.msg.cannotReduceMaxBelowCurrent(); } } this.maxThreads = maxThreads; } }
private void maybeFlush() { final int count = cachedOperations.incrementAndGet(); //no op if ( count < flushCount ) { return; } //another thread is writing, no op, just return if ( !writeSemaphore.tryAcquire() ) { return; } final long failed = entitiesFailed.get(); final long written = entitiesWritten.get(); final String message; if ( failed > 0 ) { message = "Failed to import " + failed + " entities. Successfully imported " + written + " entities"; } else { message = "Successfully imported " + written + " entities"; } updateFileImport( FileImport.State.STARTED, message ); cachedOperations.addAndGet( flushCount * -1 ); writeSemaphore.release(); }
/** * */ private void acquireRemapSemaphore() throws IgniteInterruptedCheckedException { try { if (remapSem.availablePermits() != REMAP_SEMAPHORE_PERMISSIONS_COUNT) { if (timeout == DFLT_UNLIMIT_TIMEOUT) { // Wait until failed data being processed. remapSem.acquire(REMAP_SEMAPHORE_PERMISSIONS_COUNT); remapSem.release(REMAP_SEMAPHORE_PERMISSIONS_COUNT); } else { // Wait until failed data being processed. boolean res = remapSem.tryAcquire(REMAP_SEMAPHORE_PERMISSIONS_COUNT, timeout, TimeUnit.MILLISECONDS); if (res) remapSem.release(REMAP_SEMAPHORE_PERMISSIONS_COUNT); else throw new IgniteDataStreamerTimeoutException("Data streamer exceeded timeout " + "while was waiting for failed data resending finished."); } } } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IgniteInterruptedCheckedException(e); } }
@Override public void processWatermark(Watermark mark) throws Exception { if (!semaphore.tryAcquire()) { Assert.fail("Concurrent invocation of operator functions."); } semaphore.release(); } }