What is the advantage of using ExecutorService over running threads passing a Runnable into the Thread constructor?
What is the advantage of using ExecutorService over running threads passing a Runnable into
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
ExecutorServiceabstracts away many of the complexities associated with the lower-level abstractions like rawThread. It provides mechanisms for safely starting, closing down, submitting, executing, and blocking on the successful or abrupt termination of tasks (expressed asRunnableorCallable).From JCiP, Section 6.2, straight from the horse’s mouth:
Rather than spending your time implementing (often incorrectly, and with great effort) the underlying infrastructure for parallelism, the
j.u.concurrentframework allows you to instead focus on structuring tasks, dependencies, potential parallelism. For a large swath of concurrent applications, it is straightforward to identify and exploit task boundaries and make use ofj.u.c, allowing you to focus on the much smaller subset of true concurrency challenges which may require more specialized solutions.Also, despite the boilerplate look and feel, the Oracle API page summarizing the concurrency utilities includes some really solid arguments for using them, not least:
Java concurrency in practiceis a good book on concurrency. If you haven’t already, get yourself a copy. The comprehensive approach to concurrency presented there goes well beyond this question, and will save you a lot of heartache in the long run.