protected ContextAwareThread( final Runnable task, final String name, final boolean daemonFlag, final UncaughtExceptionHandler exceptionHandler, final Map<String, String> threadContext ) { super(task, name); setDaemon(daemonFlag); setUncaughtExceptionHandler(exceptionHandler); this.threadContext = threadContext; } }
@Override public final AsyncRunnableBase await() throws InterruptedException { await(Long.MAX_VALUE, TimeUnit.MILLISECONDS); return this; }
@Override public Thread newThread(final Runnable task) { return new ContextAwareThread( task, threadNamePrefix + "#" + threadNumber.incrementAndGet(), daemonFlag, exceptionHandler, threadContext ); }
ioWorkerCount = confWorkerCount; } else if (concurrencyLimit > 0) { ioWorkerCount = Math.min(concurrencyLimit, ThreadUtil.getHardwareThreadCount()); } else { ioWorkerCount = ThreadUtil.getHardwareThreadCount();
@Override public final void close() throws IOException { REGISTRY.remove(this); super.close(); }
@Override protected final void invokeTimedExclusively(final long startTimeNanos) { try { lastValue = updateAction.call(); } catch(final Exception e) { LOG.log(Level.WARNING, "Failed to execute the value update action", e); e.printStackTrace(System.err); } }
@Override public final void run() { try(final Instance ctx = CloseableThreadContext.putAll(threadContext)) { super.run(); } catch(final Throwable cause) { LOG.log(Level.SEVERE, "Unhandled thread failure", cause); } } }
public static void closeAll() { while(!REGISTRY.isEmpty()) { try { REGISTRY.peek().close(); } catch(final Throwable cause) { LOG.log(Level.WARNING, "Failed to close the daemon instance", cause); } } } }
@Override public final AsyncRunnableBase stop() { // shutdown first shutdown(); // then stop actually stateLock.lock(); try { if(state == SHUTDOWN) { doStop(); state = STOPPED; stateChanged.signalAll(); } else { LOG.warning("Not allowed to stop while state is \"" + state + "\""); } } finally { stateLock.unlock(); } return this; }
@Override public final AsyncRunnableBase start() { stateLock.lock(); try { if(state == INITIAL || state == STOPPED) { doStart(); state = STARTED; stateChanged.signalAll(); } else { LOG.warning("Not allowed to start while state is \"" + state + "\""); } } finally { stateLock.unlock(); } return this; }
@Override public final AsyncRunnableBase shutdown() { stateLock.lock(); try { if(state == STARTED || state == INITIAL) { doShutdown(); state = SHUTDOWN; stateChanged.signalAll(); } else { LOG.warning("Not allowed to shutdown while state is \"" + state + "\""); } } finally { stateLock.unlock(); } return this; }
public FibersExecutor(final boolean backgroundFlag) { final int svcThreadCount = Runtime.getRuntime().availableProcessors(); executor = new ThreadPoolExecutor( svcThreadCount, svcThreadCount, 0, TimeUnit.DAYS, new ArrayBlockingQueue<>(1), new ContextAwareThreadFactory("fibers-executor-", true, null) ); this.backgroundFlag = backgroundFlag; for(int i = 0; i < svcThreadCount; i ++) { final FibersExecutorTask svcWorkerTask = new FibersExecutorTask( fibers, backgroundFlag ); executor.submit(svcWorkerTask); workers.add(svcWorkerTask); svcWorkerTask.start(); } }
long waitTime = timeoutInMillis; if(completed.get()) { return getResult(); } else if(waitTime <= 0) { throw new TimeoutException(); sharedLatch.await(waitTime, TimeUnit.MILLISECONDS); if(completed.get()) { return getResult(); } else { waitTime = timeoutInMillis - (System.currentTimeMillis() - startTime);
ThreadUtil.getHardwareThreadCount(), new LogContextThreadFactory("shutdownWorker", true) );
@Override public final void close() throws IOException { super.close(); REGISTRY.remove(daemonRef); }
@Override protected final void invokeTimedExclusively(final long startTimeNanos) { try { lastValue = updateAction.call(); } catch (final Exception e) { LogUtil.trace( Loggers.ERR, Level.WARN, e, "Failed to execute the value update action"); } }
@Override public final void run() { try (final Instance ctx = CloseableThreadContext.putAll(threadContext)) { super.run(); } catch (final InterruptRunException e) { throw e; } catch (final Throwable cause) { LOG.log(Level.SEVERE, "Unhandled thread failure", cause); } } }