executor에 의해 거부 된 Task
을 알고 싶습니다. 예를 들어, 거부 된 작업의 이름을 얻고 싶습니다. 그러나 RejectedExecutionHandler
에서이 작업을 수행 할 수 없습니다!RejectedExecutionHandler에서 작업 필드를 가져 오는 방법은 무엇입니까?
public static void main(String[] args) throws Exception{
ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 1L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(),
(Runnable r, ThreadPoolExecutor executor1) -> {
//TODO
//want to get the task name
System.out.println("in reject:");
});
Task t1 = new Task("t1");
Task t2 = new Task("t2");
Future<Integer> t1Fe = executor.submit(t1);
Future<Integer> t2Fe = executor.submit(t2);
System.out.println(t1Fe.get());
System.out.println(t2Fe.get());
}
static class Task implements Callable{
private String name;
public Task(String name) {
this.name = name;
}
@Override
public Object call() throws Exception {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(1);
return name;
}
}
작업은 작업에 getter를 제공하지 않는 FutureTask로 래핑됩니다. 리플렉션을 사용하여'FutureTask'의'callable' 필드에 접근 할 수는 있지만 매우 견고한 접근 방법은 아닙니다. – assylias
RejectedExecutionHandler를 재정의하고 자신을 만드십시오. 가장 좋은 방법입니다. – canillas
@canillas 그 질문의 코드에서 수행 된 작업입니다. 문제는 Runnable에서 기본 작업에 액세스하는 방법입니다. – assylias