Code example for RejectedExecutionException

0
    public void setParent(ThreadPoolExecutor tp) {
        parent = tp;
    } 
 
    public boolean force(Runnable o) {
        if ( parent.isShutdown() ) throw new RejectedExecutionException("Executor not running, can't force a command into the queue");
        return super.offer(o); //forces the item onto the queue, to be used if the task is rejected
    } 
 
    public boolean force(Runnable o, long timeout, TimeUnit unit) throws InterruptedException {
        if ( parent.isShutdown() ) throw new RejectedExecutionException("Executor not running, can't force a command into the queue");
        return super.offer(o,timeout,unit); //forces the item onto the queue, to be used if the task is rejected
    } 
 
    @Override 
    public boolean offer(Runnable o) {
      //we can't do any checks 
        if (parent==null) return super.offer(o);
        //we are maxed out on threads, simply queue the object 
        if (parent.getPoolSize() == parent.getMaximumPoolSize()) return super.offer(o);
        //we have idle threads, just add it to the queue