以下はクラスのソースコードです。
shutdownNow()
提出されていないタスクでどのように機能するかを確認したいと思いました。以下のコードで発生している問題はshutdownNow()
returnであり、送信されたのインスタンスを含む送信されたものではList<FutureTask>
ありません。List<Runnable>
List<Runnable>
PrimeProducer
In Below program I wanted to get the tasks which where not executed and their state so that I can reschedule them later. name() represents just state that I want to store.
そのため、送信されたタスクに変換できません。
class PrimeProducer implements Runnable {
private final SynchronousQueue<BigInteger> queue;
PrimeProducer(SynchronousQueue<BigInteger> queue) {
this.queue = queue;
}
public void run() {
try {
BigInteger p = BigInteger.ONE;
queue.put(p = p.nextProbablePrime());
} catch (InterruptedException consumed) {
System.out.println("Safe Exit");
Thread.currentThread().interrupt();
}
}
public String name() {
return "PrimeProducer";
}
public static void main(String[] args) throws InterruptedException,
ExecutionException {
PrimeProducer primeProducer = new PrimeProducer(
new SynchronousQueue<BigInteger>());//SynchronousQueue just to ensure it put is blocking
ExecutorService executorService = Executors.newFixedThreadPool(1);
executorService.submit(primeProducer);
executorService.submit(primeProducer);
List<Runnable> list = executorService.shutdownNow();
//PrimeProducer producer = (PrimeProducer) list.get(0);// Class Cast
// Exception
FutureTask<PrimeProducer> futureTask = (FutureTask<PrimeProducer>) list
.get(0);
System.out.println(futureTask.isDone());//Prints false
futureTask.get().name();//futureTask-->PrimeProducer get() hangs.
}
}
問題のある行
//PrimeProducer producer = (PrimeProducer) list.get(0);// Class Cast
// Exception
FutureTask<PrimeProducer> futureTask = (FutureTask<PrimeProducer>) list
.get(0);
futureTask.get().name();//futureTask-->PrimeProducer get() hangs.