private void schedule(Runnable command) { long timeout = 0; TimeUnit timeUnit = TimeUnit.SECONDS; if (command instanceof TimeoutRunnable) { TimeoutRunnable timeoutRunnable = ((TimeoutRunnable) command); timeout = timeoutRunnable.getTimeout(); timeUnit = timeoutRunnable.getTimeUnit(); } boolean offered; try { if (timeout == 0) { offered = workQueue.offer(command); } else { offered = workQueue.offer(command, timeout, timeUnit); } } catch (InterruptedException e) { currentThread().interrupt(); throw new RejectedExecutionException("Thread is interrupted while offering work"); } if (!offered) { throw new RejectedExecutionException("Task: " + command + " is rejected, the worker queue is full!"); } }
private void schedule(Runnable task) { if (!started.get() && started.compareAndSet(false, true)) { start(); } long timeoutNanos = timeoutNanos(task); try { boolean offered = timeoutNanos == 0 ? taskQueue.offer(task) : taskQueue.offer(task, timeoutNanos, NANOSECONDS); if (!offered) { throw new RejectedExecutionException("Task: " + task + " is rejected, " + "the taskqueue of " + getName() + " is full!"); } } catch (InterruptedException e) { currentThread().interrupt(); throw new RejectedExecutionException("Thread is interrupted while offering work"); } }
/** * Shuts down this StripedExecutor. * <p> * No checking is done to see if the StripedExecutor already is shut down, so it should be called only once. * <p> * If there is any pending work, it will be thrown away. */ public void shutdown() { live = false; for (Worker worker : workers) { worker.workQueue.clear(); worker.interrupt(); } }
public StripedExecutor(ILogger logger, String threadNamePrefix, int threadCount, int queueCapacity, boolean lazyThreads) { checkPositive(threadCount, "threadCount should be positive but found " + threadCount); checkPositive(queueCapacity, "queueCapacity should be positive but found " + queueCapacity); this.logger = logger; this.size = threadCount; this.workers = new Worker[threadCount]; // `queueCapacity` is the given max capacity for this executor. Each worker in this executor should consume // only a portion of that capacity. Otherwise we will have `threadCount * queueCapacity` instead of // `queueCapacity`. int perThreadMaxQueueCapacity = (int) ceil(1D * queueCapacity / threadCount); for (int i = 0; i < threadCount; i++) { Worker worker = new Worker(threadNamePrefix, perThreadMaxQueueCapacity); if (!lazyThreads) { worker.started.set(true); worker.start(); } workers[i] = worker; } }
@Override public void run() { for (; ; ) { try { try { Runnable task = workQueue.take(); process(task); } catch (InterruptedException e) { if (!live) { return; } } } catch (Throwable t) { //This should not happen because the process method is protected against failure. //So if this happens, something very seriously is going wrong. logger.severe(getName() + " caught an exception", t); } } }
@Override public void run() { try { while (live) { try { Runnable task = taskQueue.take(); process(task); } catch (InterruptedException ignore) { // we can safely ignore this exception since we'll check if the // striped executor is still alive in the next iteration of the loop. ignore(ignore); } } } catch (Throwable t) { //This should not happen because the process method is protected against failure. //So if this happens, something very seriously is going wrong. logger.severe(getName() + " caught an exception", t); } }
public StripedExecutor(ILogger logger, String threadNamePrefix, int threadCount, int maximumQueueCapacity) { checkPositive(threadCount, "threadCount should be positive but found " + threadCount); checkPositive(maximumQueueCapacity, "maximumQueueCapacity should be positive but found " + maximumQueueCapacity); this.logger = logger; this.size = threadCount; this.workers = new Worker[threadCount]; // `maximumQueueCapacity` is the given max capacity for this executor. Each worker in this executor should consume // only a portion of that capacity. Otherwise we will have `threadCount * maximumQueueCapacity` instead of // `maximumQueueCapacity`. final int perThreadMaxQueueCapacity = (int) ceil(1D * maximumQueueCapacity / threadCount); for (int i = 0; i < threadCount; i++) { Worker worker = new Worker(threadNamePrefix, perThreadMaxQueueCapacity); worker.start(); workers[i] = worker; } }
private void process(Runnable task) { processed.inc(); try { task.run(); } catch (Throwable e) { OutOfMemoryErrorDispatcher.inspectOutOfMemoryError(e); logger.severe(getName() + " caught an exception while processing task:" + task, e); } }
private void shutdown() { taskQueue.clear(); interrupt(); } }
private void process(Runnable task) { processed.inc(); try { task.run(); } catch (Throwable e) { OutOfMemoryErrorDispatcher.inspectOutOfMemoryError(e); logger.severe(getName() + " caught an exception while processing:" + task, e); } }