Code from Java concurrency in practice book Listing 8.1
Why is the code deadlocking? Is it because the rpt.call in main() is basically the same thread as that in Executors?
Even if I use 10 thread for exec = Executors.newFixedThreadPool(10); it still deadlocks?
public class ThreadDeadlock {
ExecutorService exec = Executors.newSingleThreadExecutor();
public class RenderPageTask implements Callable<String> {
public String call() throws Exception {
Future<String> header, footer;
header = exec.submit(new LoadFileTask("header.html"));
footer = exec.submit(new LoadFileTask("footer.html"));
String page = renderBody();
// Will deadlock -- task waiting for result of subtask
return header.get() + page + footer.get();
}
}
public static void main(String [] args ) throws Exception {
ThreadDeadlock td = new ThreadDeadlock();
ThreadDeadlock.RenderPageTask rpt = td.new RenderPageTask();
rpt.call();
}
}