아무도 나에게 예제를 제공 할 수있는 사람이 없습니다. RejectedExecutionException 아마도 실제의 예입니다. 미리 감사드립니다. RejectedExecutionException을 얻으려면 어떻게해야합니까?
답변
RejectedExecutionException을 얻는 예제를 제공 할 수있는 사람은 누구나 가능합니다. 실제 사례 일 수 있습니다.
확실히. 다음 코드는 스레드가 하나만 실행되는 스레드 풀에 2 개의 작업을 전송합니다. SynchronousQueue
을 사용합니다. 이는 작업 대기열에 작업이 저장되지 않음을 의미합니다.
각 작업이 실행되는 데 시간이 걸리므로 2 차 실행이 대기열을 채우고 RejectedExecutionException
을 던집니다.
// create a 1 thread pool with no buffer for the runnable jobs
ExecutorService threadPool =
new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
new SynchronousQueue<Runnable>());
// submit 2 jobs that take a while to run
/// this job takes the only thread
threadPool.execute(new SleepRunnable());
// this tries to put the job into the queue, throws RejectedExecutionException
threadPool.execute(new SleepRunnable());
public class SleepRunnable implements Runnable {
public void run() {
try {
// this just sleeps for a while which pauses the thread
Thread.sleep(10000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
}
당신은 이것이 무슨 일을하는 단어로 설명 할 수 있습니까? 왜 미래 사용자를위한 참조를 할 수 있습니까? – hexafraction
Done @hexafraction. – Gray
완벽한, 감사합니다! – hexafraction
shutdown(
을 호출 한 후 실행 프로그램에 작업을 보내는 것은이 예외를 throw합니다.
또한 큐가 꽉 찬 경우 실행 프로그램이 제한된 블로킹 큐를 사용하면 작업이 차단되지는 않지만 예외가 발생하면 실패 할 것입니다.
이 질문은 이미 질문과 답했습니다 What could be the cause of RejectedExecutionException Submitting tasks to a thread-pool gives RejectedExecutionException
우리가 작업을 시작하려고 때문에이 코드는 당신에게 오류를 제공하지만 집행은 위의 링크를 참조 할 수 있습니다 종료 추가 설명을 위해 대답은 꽤 완성 된 것으로 보입니다.
public class Executorz {
public static void main(String[] args) {
Executorz ex = new Executorz();
ExecutorService es = Executors.newFixedThreadPool(10);
for (int i = 0; i<100 ; i++){
System.out.println("Executed");
es.execute(ex.getNewCountin());
if (i==20)
es.shutdown();
}
}
public Countin getNewCountin(){
return new Countin();
}
public class Countin implements Runnable {
@Override
public void run() {
for (double i =0; i<1000000000 ; i++){
}
System.out.println("Done");
}
}
}
실행 프로그램을 종료하고 새 작업을 제출하십시오. – jtahlborn
'throw new RejectedExecutionException(); ' – Renan
LOL !!! Classic @ Renan. – Gray