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



Yüklə 0,82 Mb.
Pdf görüntüsü
səhifə18/20
tarix13.12.2023
ölçüsü0,82 Mb.
#176388
1   ...   12   13   14   15   16   17   18   19   20
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 

Yüklə 0,82 Mb.

Dostları ilə paylaş:
1   ...   12   13   14   15   16   17   18   19   20




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin