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"); } }
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; } }
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; } }