private long timeoutNanos(Runnable task) { if (task instanceof TimeoutRunnable) { TimeoutRunnable r = ((TimeoutRunnable) task); return r.getTimeUnit().toNanos(r.getTimeout()); } else { return 0; } }
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!"); } }