Failed to get CallableThread in RejectionHandler

I have a thread pool in which there will be a Callableworker thread with RejectionHandler. I need to complete this Callable's task RejectionHandler, but could not get it.

In this example below, I need a unique Callable task for which a RejectionHandler is running. The RejecitonHandler Runnableexhibit FutureTaskis where I expect it to be sent to the workflow Callable.

Please help me get Callablea working line instance in RejectionHandler.

import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class RejectionDemo {
    RejectionDemo(){
        Random random = new Random();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2), new RejectionHandlerImpl());
        CallableWorkerThread workers[] = 
                new CallableWorkerThread[10];
        for (int i=0; i< workers.length; i++){
            workers[i] = new CallableWorkerThread(random.nextInt(100));
            FutureTask<Integer> task = new FutureTask<Integer>(workers[i]);
            executor.submit(task);
        }
    }

    public static void main(String args[]){
        RejectionDemo demo = new RejectionDemo();
    }
    public class CallableWorkerThread implements
        Callable<Integer> {
        private int uniqueId;

        CallableWorkerThread(int uniqueId) {
            this.uniqueId = uniqueId;
        }

        public Integer call() {
            System.out.println("Unique id="+uniqueId);
            return uniqueId;
        }
        public String toString(){
            return ""+uniqueId;
        }
    }
    class RejectionHandlerImpl implements RejectedExecutionHandler{
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            try{
                System.out.println(r);
            }catch(Throwable t){
                t.printStackTrace();
            }
        }
    }
}

Output

java.util.concurrent.FutureTask@70036428
Unique id=68
java.util.concurrent.FutureTask@6ea4b78b
java.util.concurrent.FutureTask@e3f6d
java.util.concurrent.FutureTask@1ce84763
java.util.concurrent.FutureTask@55a6c368
java.util.concurrent.FutureTask@4e77b794
java.util.concurrent.FutureTask@15b57dcb
Unique id=55
Unique id=83

I expect CallableWorkerThread instead of FutureTask. Help me create an instance of WorkerThread.

+1
1

workers[i] = new CallableWorkerThread(random.nextInt(100));
FutureTask<Integer> task = new FutureTask<Integer>(workers[i]);
executor.submit(task);

FutureTask, CallableWorkerThread, submit, Runnable FutureTask, Runnable.

, FutureTask FutureTask. :

  • workers[i] = new CallableWorkerThread(random.nextInt(100));
    executor.submit(workers[i]);
    

    ExecutorService Callable FutureTask.

  • workers[i] = new CallableWorkerThread(random.nextInt(100));
    executor.execute(new FutureTask<Integer>(workers[i]));
    

    Callable Runnable ( execute, submit)

Callable, , FutureTask:

static class MyFutureTask<T> extends FutureTask<T> {
    final Callable<T> theCallable;

    public MyFutureTask(Callable<T> callable) {
        super(callable);
        theCallable=callable;
    }
}

:

    for (int i=0; i< workers.length; i++){
        workers[i] = new CallableWorkerThread(random.nextInt(100));
        executor.execute(new MyFutureTask<Integer>(workers[i]));
    }

RejectedExecutionHandler:

class RejectionHandlerImpl implements RejectedExecutionHandler{
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        if(r instanceof MyFutureTask) {
            MyFutureTask<?> myFutureTask = (MyFutureTask)r;
            Callable<?> c=myFutureTask.theCallable;
            System.out.println(c);
        }
        else System.out.println(r);
    }
}
+1

Source: https://habr.com/ru/post/1668962/


All Articles