Multithreading Interview Questions in Java for Freshers What are the benefits of using Multithreading? 2


Download 0.82 Mb.
Pdf ko'rish
bet18/20
Sana14.11.2023
Hajmi0.82 Mb.
#1772108
1   ...   12   13   14   15   16   17   18   19   20
Bog'liq
Threads

public void 
uncaughtException
(Thread t, Throwable e)
{
System.out.println(
"exception occured:"
+e.getMessage());
}
}; 
38. What is the ExecutorService interface?
ExecutorService interface is basically a sub-interface of Executor interface with some
additional methods or features that help in managing and controlling the execution
of threads. It enables us to execute tasks asynchronously on threads.
Page 31
© Copyright by Interviewbit


Multithreading Interview Questions
Example: 
Page 32
© Copyright by Interviewbit


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 

Download 0.82 Mb.

Do'stlaringiz bilan baham:
1   ...   12   13   14   15   16   17   18   19   20




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling