in Education by
We have a service method GetDataParallel( ) which maybe called by many clients currently, and we use the ExecutorService to called the MyCallable inside it. However I found unless I called the executorService.shutdown(); the application never exit, so why the application cannot exit , we must shut down all thread pool threads manually before the application exit? and in service environment I think we don't need to call the executorService.shutdown(); to keep the application alive, right ? import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class MultiThreading { static ExecutorService executorService = Executors.newFixedThreadPool(100); private List _BusinessUnits= new ArrayList(); public static void main(String[] args) throws Exception { MultiThreading kl =new MultiThreading(); kl.GetDataParallel(); Thread.sleep(10000); System.out.println("111111111"); //executorService.shutdown(); } public void GetDataParallel( ) throws Exception { _BusinessUnits.add("BU1"); _BusinessUnits.add("BU2"); _BusinessUnits.add("BU3"); for(final String v : _BusinessUnits) { ExecutorServiceTest.executorService.submit( new MyCallable()); } } } class MyCallable implements Callable { @Override public String call() throws Exception { Thread.sleep(1000); //return the thread name executing this callable task System.out.println(Thread.currentThread().getName()); return Thread.currentThread().getName(); } } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Typically you shut down an ExecutorService when the application is exiting - most applications have some sort of lifecycle and shutdown sequence. If you expect your application to exit when the main thread completes whatever it has to do, you want to shut it down when its work is done (meaning there's a well-defined point at which it's work is done, and you can tell when that is). Server-side frameworks frequently have lifecycle methods that you can hook into to detect when your code is being shut down and cleanly exit. Or you can use VM shutdown hooks (perhaps to delay shutdown until all currently queued jobs are complete), so that no matter what code causes the program to exit, your cleanup code will be run. In general, it's a good idea to create a well-defined exit point if there isn't one provided by a framework - it lets you have an application that can be cleanly unloaded (and perhaps, reloaded with updated configuration) without the VM necessarily shutting down at all - I've used that trick to be able to have a server application reconfigure itself and reload with zero downtime in response to a Unix signal. So, the simple answer to your question is "When it isn't going to be used anymore". In almost any application there is a point at which that's unambiguously true. BTW, to contradict one of the other responders, an ExecutorService can use daemon threads - you can provide a ThreadFactory that configures threads however you want before you start them. But I'd encourage you to not use daemon threads and explicitly shut down the thread pool at a well-defined point - that's not the typical practice, but it will both mean your code has the potential to be shut down cleanly, and it will encourage you to think about lifecycle, which is likely to lead to better code.

Related questions

0 votes
    In my Java application I want to have a pool of threads (I use ExecutorService for this) that connect ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    The class Collector was created to generate the map with values (for example). Need that Collector contains the resource, where haves ... HashMap()); for (int i = id * 100; i...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    The class Collector was created to generate the map with values (for example). Need that Collector contains the resource, where haves ... HashMap()); for (int i = id * 100; i...
asked May 27, 2022 in Education by JackTerrance
0 votes
    I'm trying to make sure that all my threads of Class A have finished before notify is called. At ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I am having my web application deployed on Tomcat5.5 and I use it in integration with eclipse 3.2 ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    Std 9th The first generation computers used to shutdown because of ___________ . please answer quickly Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    Is there any reason to keep time series data in its own database, separate from other tables or is ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I want to implement in Java a class for handling graph data structures. I have a Node class and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    What data structure should be used when number of elements is fixed? (a) Array (b) Array list (c) Vector ... & Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    I am training on 970 samples and validating on 243 samples. How big should batch size and number of epochs be ... on data input size? Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    I am using exit(), quit(), os._exit(), sys.exit() to stop script execution, and I am confused which ... use to stop script execution? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    Give scientific reasons: Antibiotics should be taken only when prescribed by a doctor. Select the correct ... proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 7, 2021 in Education by JackTerrance
0 votes
    What data structure should be used when number of elements is fixed? (a) Array (b) Array list (c ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    _________ refers to an undesirable state when a system attempts to perform two or more operations, which should, ... condition 2. Optimistic locking 3. Locking 4. Transaction...
asked Apr 12, 2021 in Technology by JackTerrance
...