Multithreading Interview Questions
Output:
Shutdown executor
shutdown finished
Page 33
© Copyright by Interviewbit
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class
TestThread
{
public static void
main
(
final String[] arguments)
th
ExecutorService e = Executors.newSingleThreadExecutor();
try {
e.submit(
new Thread());
System.out.println(
"Shutdown executor"
);
e.shutdown();
e.awaitTermination(
5
, TimeUnit.SECONDS);
}
catch (InterruptedException ex) {
System.err.println(
"tasks interrupted"
);
}
finally {
if (!e.isTerminated()) {
System.err.println(
"cancel non-finished tasks"
);
}
e.shutdownNow();
System.out.println(
"shutdown finished"
);
}
}
static class
Dostları ilə paylaş: