この質問は、それに至るまでのいくつかの質問の集大成です。行った変更をコミットする前に、すべてが正しいことを確認したいだけです。
だからここに私がデータ構造のために取り組んでいるものがあります:
抽象クラスでは:
public abstract void doRun();
public void run(){
try{
while(!Thread.currentThread().isInterrupted() || hasRunBefore){
doRun();
}
}catch (InterruptedException e){Thread.sleep(1); System.out.println("Thread Interrupted: "); e.printStackTrace(); }
}
次に、私の抽象クラスを実装している子クラスで:
public void doRun(){
doChildClassStuff();
}
次に、メインクラスでこれらを呼び出すときは、単純に行います
ClassName myClass = new ClassName(constructor.list);
ExecutorService threadPool = Executors.newFixedThreadPool(12);
List<Future<?>> taskList = new ArrayList<Future<?>>();
taskList.add(myClass);
for(Future future : taskList){
try{
future.get(1, TimeUnit.SECONDS);
}catch(CancellationException cx){ System.err.println("Cancellation Exception: "); cx.printStackTrace();
}catch(ExecutionException ex){ System.err.println("Execution Exception: ");ex.printStackTrace();
}catch(InterruptedException ix){ System.err.println("Interrupted Exception: ");ix.printStackTrace();
}catch(TimeoutException ex) {future.cancel(true);}
}
threadPool.shutdown();
threadPool.awaitTermination(60, TimeUnit.SECONDS);
threadPool.shutdownNow();
thread.sleep() が失敗した場合は、スレッドが中断され、終了する必要があることを意味します。すべてのスレッドが 60 秒以上かかる場合、すべてのスレッドに awaitTermination から thread.interrupt() コマンドが送信されますよね?
私はここで完全に基地外ですか?